home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / src / keyboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-25  |  175.5 KB  |  6,199 lines

  1. /* Keyboard and mouse input; editor command loop.
  2.    Copyright (C) 1985,86,87,88,89,93,94 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Allow config.h to undefine symbols found here.  */
  21. #include <signal.h>
  22.  
  23. #include <config.h>
  24. #include <stdio.h>
  25. #undef NULL
  26. #include "termchar.h"
  27. #include "termopts.h"
  28. #include "lisp.h"
  29. #include "termhooks.h"
  30. #include "macros.h"
  31. #include "frame.h"
  32. #include "window.h"
  33. #include "commands.h"
  34. #include "buffer.h"
  35. #include "disptab.h"
  36. #include "dispextern.h"
  37. #include "keyboard.h"
  38. #include "intervals.h"
  39. #include "blockinput.h"
  40. #include <setjmp.h>
  41. #include <errno.h>
  42.  
  43. #ifdef MSDOS
  44. #include "msdos.h"
  45. #include <time.h>
  46. #else /* not MSDOS */
  47. #ifndef VMS
  48. #include <sys/ioctl.h>
  49. #endif
  50. #endif /* not MSDOS */
  51.  
  52. #include "syssignal.h"
  53. #include "systty.h"
  54. #include "systime.h"
  55.  
  56. /* This is to get the definitions of the XK_ symbols.  */
  57. #ifdef HAVE_X_WINDOWS
  58. #include "xterm.h"
  59. #endif
  60.  
  61. #ifdef AMIGA
  62. #define FAR far
  63. #else
  64. #define FAR
  65. #endif
  66.  
  67. #ifdef USE_PROTOS
  68. #include "protos.h"
  69. #endif
  70.  
  71. #ifdef USE_X_TOOLKIT /* CHFIXME */
  72. #define USE_EXTERNAL_MENU_BAR
  73. #endif
  74.  
  75. extern int errno;
  76.  
  77. /* Variables for blockinput.h: */
  78.  
  79. /* Non-zero if interrupt input is blocked right now.  */
  80. int interrupt_input_blocked;
  81.  
  82. /* Nonzero means an input interrupt has arrived
  83.    during the current critical section.  */
  84. int interrupt_input_pending;
  85.  
  86.  
  87. #ifdef HAVE_X_WINDOWS
  88. extern Lisp_Object Vmouse_grabbed;
  89.  
  90. /* Make all keyboard buffers much bigger when using X windows.  */
  91. #define KBD_BUFFER_SIZE 4096
  92. #else    /* No X-windows, character input */
  93. #define KBD_BUFFER_SIZE 256
  94. #endif    /* No X-windows */
  95.  
  96. /* Following definition copied from eval.c */
  97.  
  98. struct backtrace
  99.   {
  100.     struct backtrace *next;
  101.     Lisp_Object *function;
  102.     Lisp_Object *args;    /* Points to vector of args. */
  103.     int nargs;        /* length of vector.  If nargs is UNEVALLED,
  104.                args points to slot holding list of
  105.                unevalled args */
  106.     char evalargs;
  107.   };
  108.  
  109. /* Non-nil disable property on a command means
  110.    do not execute it; call disabled-command-hook's value instead.  */
  111. Lisp_Object Qdisabled, Qdisabled_command_hook;
  112.  
  113. #define NUM_RECENT_KEYS (100)
  114. int recent_keys_index;    /* Index for storing next element into recent_keys */
  115. int total_keys;        /* Total number of elements stored into recent_keys */
  116. Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
  117.  
  118. /* Vector holding the key sequence that invoked the current command.
  119.    It is reused for each command, and it may be longer than the current
  120.    sequence; this_command_key_count indicates how many elements
  121.    actually mean something.
  122.    It's easier to staticpro a single Lisp_Object than an array.  */
  123. Lisp_Object this_command_keys;
  124. int this_command_key_count;
  125.  
  126. extern int minbuf_level;
  127.  
  128. extern struct backtrace *backtrace_list;
  129.  
  130. /* Nonzero means do menu prompting.  */
  131. static int menu_prompting;
  132.  
  133. /* Character to see next line of menu prompt.  */
  134. static Lisp_Object menu_prompt_more_char;
  135.  
  136. /* For longjmp to where kbd input is being done.  */
  137. static jmp_buf getcjmp;
  138.  
  139. /* True while doing kbd input.  */
  140. int waiting_for_input;
  141.  
  142. /* True while displaying for echoing.   Delays C-g throwing.  */
  143. static int echoing;
  144.  
  145. /* Nonzero means C-g should cause immediate error-signal.  */
  146. int immediate_quit;
  147.  
  148. /* Character to recognize as the help char.  */
  149. Lisp_Object Vhelp_char;
  150.  
  151. /* Form to execute when help char is typed.  */
  152. Lisp_Object Vhelp_form;
  153.  
  154. /* Command to run when the help character follows a prefix key.  */
  155. Lisp_Object Vprefix_help_command;
  156.  
  157. /* List of items that should move to the end of the menu bar.  */
  158. Lisp_Object Vmenu_bar_final_items;
  159.  
  160. /* Character that causes a quit.  Normally C-g.
  161.  
  162.    If we are running on an ordinary terminal, this must be an ordinary
  163.    ASCII char, since we want to make it our interrupt character.
  164.  
  165.    If we are not running on an ordinary terminal, it still needs to be
  166.    an ordinary ASCII char.  This character needs to be recognized in
  167.    the input interrupt handler.  At this point, the keystroke is
  168.    represented as a struct input_event, while the desired quit
  169.    character is specified as a lispy event.  The mapping from struct
  170.    input_events to lispy events cannot run in an interrupt handler,
  171.    and the reverse mapping is difficult for anything but ASCII
  172.    keystrokes.
  173.  
  174.    FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
  175.    ASCII character.  */
  176. int quit_char;
  177.  
  178. extern Lisp_Object current_global_map;
  179. extern int minibuf_level;
  180.  
  181. /* If non-nil, this is a map that overrides all other local maps.  */
  182. Lisp_Object Voverriding_local_map;
  183.  
  184. /* Current depth in recursive edits.  */
  185. int command_loop_level;
  186.  
  187. /* Total number of times command_loop has read a key sequence.  */
  188. int num_input_keys;
  189.  
  190. /* Last input character read as a command.  */
  191. Lisp_Object last_command_char;
  192.  
  193. /* Last input character read as a command, not counting menus
  194.    reached by the mouse.  */
  195. Lisp_Object last_nonmenu_event;
  196.  
  197. /* Last input character read for any purpose.  */
  198. Lisp_Object last_input_char;
  199.  
  200. /* If not Qnil, a list of objects to be read as subsequent command input.  */
  201. Lisp_Object Vunread_command_events;
  202.  
  203. /* If not -1, an event to be read as subsequent command input.  */
  204. int unread_command_char;
  205.  
  206. /* If not Qnil, this is a switch-frame event which we decided to put
  207.    off until the end of a key sequence.  This should be read as the
  208.    next command input, after any unread_command_events.
  209.  
  210.    read_key_sequence uses this to delay switch-frame events until the
  211.    end of the key sequence; Fread_char uses it to put off switch-frame
  212.    events until a non-ASCII event is acceptable as input.  */
  213. Lisp_Object unread_switch_frame;
  214.  
  215. /* A mask of extra modifier bits to put into every keyboard char.  */
  216. int extra_keyboard_modifiers;
  217.  
  218. /* Char to use as prefix when a meta character is typed in.
  219.    This is bound on entry to minibuffer in case ESC is changed there.  */
  220.  
  221. Lisp_Object meta_prefix_char;
  222.  
  223. /* Last size recorded for a current buffer which is not a minibuffer.  */
  224. static int last_non_minibuf_size;
  225.  
  226. /* Number of idle seconds before an auto-save and garbage collection.  */
  227. static Lisp_Object Vauto_save_timeout;
  228.  
  229. /* Total number of times read_char has returned.  */
  230. int num_input_chars;
  231.  
  232. /* Total number of times read_char has returned, outside of macros.  */
  233. int num_nonmacro_input_chars;
  234.  
  235. /* Auto-save automatically when this many characters have been typed
  236.    since the last time.  */
  237.  
  238. static int auto_save_interval;
  239.  
  240. /* Value of num_nonmacro_input_chars as of last auto save.  */
  241.  
  242. int last_auto_save;
  243.  
  244. /* Last command executed by the editor command loop, not counting
  245.    commands that set the prefix argument.  */
  246.  
  247. Lisp_Object last_command;
  248.  
  249. /* The command being executed by the command loop.
  250.    Commands may set this, and the value set will be copied into last_command
  251.    instead of the actual command.  */
  252. Lisp_Object this_command;
  253.  
  254. /* The value of point when the last command was executed.  */
  255. int last_point_position;
  256.  
  257. /* The buffer that was current when the last command was started.  */
  258. Lisp_Object last_point_position_buffer;
  259.  
  260. #ifdef MULTI_FRAME
  261. /* The frame in which the last input event occurred, or Qmacro if the
  262.    last event came from a macro.  We use this to determine when to
  263.    generate switch-frame events.  This may be cleared by functions
  264.    like Fselect_frame, to make sure that a switch-frame event is
  265.    generated by the next character.  */
  266. Lisp_Object internal_last_event_frame;
  267. #endif
  268.  
  269. /* A user-visible version of the above, intended to allow users to
  270.    figure out where the last event came from, if the event doesn't
  271.    carry that information itself (i.e. if it was a character).  */
  272. Lisp_Object Vlast_event_frame;
  273.  
  274. /* The timestamp of the last input event we received from the X server.
  275.    X Windows wants this for selection ownership.  */
  276. unsigned long last_event_timestamp;
  277.  
  278. Lisp_Object Qself_insert_command;
  279. Lisp_Object Qforward_char;
  280. Lisp_Object Qbackward_char;
  281. Lisp_Object Qundefined;
  282.  
  283. /* read_key_sequence stores here the command definition of the
  284.    key sequence that it reads.  */
  285. Lisp_Object read_key_sequence_cmd;
  286.  
  287. /* Form to evaluate (if non-nil) when Emacs is started.  */
  288. Lisp_Object Vtop_level;
  289.  
  290. /* User-supplied string to translate input characters through.  */
  291. Lisp_Object Vkeyboard_translate_table;
  292.  
  293. /* Keymap mapping ASCII function key sequences onto their preferred forms.  */
  294. extern Lisp_Object Vfunction_key_map;
  295.  
  296. /* Keymap mapping ASCII function key sequences onto their preferred forms.  */
  297. Lisp_Object Vkey_translation_map;
  298.  
  299. /* Non-nil means deactivate the mark at end of this command.  */
  300. Lisp_Object Vdeactivate_mark;
  301.  
  302. /* Menu bar specified in Lucid Emacs fashion.  */
  303.  
  304. Lisp_Object Vlucid_menu_bar_dirty_flag;
  305. Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
  306.  
  307. /* Hooks to run before and after each command.  */
  308. Lisp_Object Qpre_command_hook, Qpost_command_hook;
  309. Lisp_Object Vpre_command_hook, Vpost_command_hook;
  310. Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
  311.  
  312. /* File in which we write all commands we read.  */
  313. FILE *dribble;
  314.  
  315. /* Nonzero if input is available.  */
  316. int input_pending;
  317.  
  318. /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
  319.    keep 0200 bit in input chars.  0 to ignore the 0200 bit.  */
  320.  
  321. int meta_key;
  322.  
  323. extern char *pending_malloc_warning;
  324.  
  325. /* Circular buffer for pre-read keyboard input.  */
  326. static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
  327.  
  328. /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
  329.  
  330.    The interrupt-level event handlers will never enqueue an event on a
  331.    frame which is not in Vframe_list, and once an event is dequeued,
  332.    internal_last_event_frame or the event itself points to the frame.
  333.    So that's all fine.
  334.  
  335.    But while the event is sitting in the queue, it's completely
  336.    unprotected.  Suppose the user types one command which will run for
  337.    a while and then delete a frame, and then types another event at
  338.    the frame that will be deleted, before the command gets around to
  339.    it.  Suppose there are no references to this frame elsewhere in
  340.    Emacs, and a GC occurs before the second event is dequeued.  Now we
  341.    have an event referring to a freed frame, which will crash Emacs
  342.    when it is dequeued.
  343.  
  344.    Similar things happen when an event on a scroll bar is enqueued; the
  345.    window may be deleted while the event is in the queue.
  346.  
  347.    So, we use this vector to protect the frame_or_window field in the
  348.    event queue.  That way, they'll be dequeued as dead frames or
  349.    windows, but still valid lisp objects.
  350.  
  351.    If kbd_buffer[i].kind != no_event, then
  352.      (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
  353.       == kbd_buffer[i].frame_or_window.  */
  354. static Lisp_Object kbd_buffer_frame_or_window;
  355.  
  356. /* Pointer to next available character in kbd_buffer.
  357.    If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
  358.    This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
  359.    next available char is in kbd_buffer[0].  */
  360. #ifndef AMIGA
  361. static
  362. #endif
  363. struct input_event *kbd_fetch_ptr;
  364.  
  365. /* Pointer to next place to store character in kbd_buffer.  This
  366.    may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
  367.    character should go in kbd_buffer[0].  */
  368. #ifdef __STDC__
  369. volatile
  370. #endif
  371. #ifndef AMIGA
  372. static
  373. #endif
  374. struct input_event *kbd_store_ptr;
  375.  
  376. /* The above pair of variables forms a "queue empty" flag.  When we
  377.    enqueue a non-hook event, we increment kbd_write_count.  When we
  378.    dequeue a non-hook event, we increment kbd_read_count.  We say that
  379.    there is input available iff the two counters are not equal.
  380.  
  381.    Why not just have a flag set and cleared by the enqueuing and
  382.    dequeuing functions?  Such a flag could be screwed up by interrupts
  383.    at inopportune times.  */
  384.  
  385. /* If this flag is non-zero, we check mouse_moved to see when the
  386.    mouse moves, and motion events will appear in the input stream.  If
  387.    it is zero, mouse motion is ignored.  */
  388. static int do_mouse_tracking;
  389.  
  390. /* The window system handling code should set this if the mouse has
  391.    moved since the last call to the mouse_position_hook.  Calling that
  392.    hook should clear this.  Code assumes that if this is set, it can
  393.    call mouse_position_hook to get the promised position, so don't set
  394.    it unless you're prepared to substantiate the claim!  */
  395. int mouse_moved;
  396.  
  397. /* True iff there is an event in kbd_buffer, or if mouse tracking is
  398.    enabled and there is a new mouse position in the mouse movement
  399.    buffer.  Note that if this is false, that doesn't mean that there
  400.    is readable input; all the events in the queue might be button-up
  401.    events, and do_mouse_tracking might be off.  */
  402. #define EVENT_QUEUES_EMPTY \
  403.   ((kbd_fetch_ptr == kbd_store_ptr) && (!do_mouse_tracking || !mouse_moved))
  404.  
  405.  
  406. /* Symbols to head events.  */
  407. Lisp_Object Qmouse_movement;
  408. Lisp_Object Qscroll_bar_movement;
  409. Lisp_Object Qswitch_frame;
  410.  
  411. /* Symbols to denote kinds of events.  */
  412. Lisp_Object Qfunction_key;
  413. Lisp_Object Qmouse_click;
  414. /* Lisp_Object Qmouse_movement; - also an event header */
  415.  
  416. /* Properties of event headers.  */
  417. Lisp_Object Qevent_kind;
  418. Lisp_Object Qevent_symbol_elements;
  419.  
  420. Lisp_Object Qmenu_enable;
  421.  
  422. /* An event header symbol HEAD may have a property named
  423.    Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
  424.    BASE is the base, unmodified version of HEAD, and MODIFIERS is the
  425.    mask of modifiers applied to it.  If present, this is used to help
  426.    speed up parse_modifiers.  */
  427. Lisp_Object Qevent_symbol_element_mask;
  428.  
  429. /* An unmodified event header BASE may have a property named
  430.    Qmodifier_cache, which is an alist mapping modifier masks onto
  431.    modified versions of BASE.  If present, this helps speed up
  432.    apply_modifiers.  */
  433. Lisp_Object Qmodifier_cache;
  434.  
  435. /* Symbols to use for parts of windows.  */
  436. Lisp_Object Qmode_line;
  437. Lisp_Object Qvertical_line;
  438. Lisp_Object Qvertical_scroll_bar;
  439. Lisp_Object Qmenu_bar;
  440.  
  441. extern Lisp_Object Qmenu_enable;
  442.  
  443. Lisp_Object recursive_edit_unwind (), command_loop ();
  444. Lisp_Object Fthis_command_keys ();
  445. Lisp_Object Qextended_command_history;
  446.  
  447. Lisp_Object Qpolling_period;
  448.  
  449. /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
  450.    happens.  */
  451. EMACS_TIME *input_available_clear_time;
  452.  
  453. /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
  454.    Default is 1 if INTERRUPT_INPUT is defined.  */
  455. int interrupt_input;
  456.  
  457. /* Nonzero while interrupts are temporarily deferred during redisplay.  */
  458. int interrupts_deferred;
  459.  
  460. /* nonzero means use ^S/^Q for flow control.  */
  461. int flow_control;
  462.  
  463. /* Allow m- file to inhibit use of FIONREAD.  */
  464. #ifdef BROKEN_FIONREAD
  465. #undef FIONREAD
  466. #endif
  467.  
  468. /* We are unable to use interrupts if FIONREAD is not available,
  469.    so flush SIGIO so we won't try.  */
  470. #ifndef FIONREAD
  471. #ifdef SIGIO
  472. #undef SIGIO
  473. #endif
  474. #endif
  475.  
  476. /* If we support X Windows, turn on the code to poll periodically
  477.    to detect C-g.  It isn't actually used when doing interrupt input.  */
  478. #ifdef HAVE_X_WINDOWS
  479. #define POLL_FOR_INPUT
  480. #endif
  481.  
  482. /* Global variable declarations.  */
  483.  
  484. /* Function for init_keyboard to call with no args (if nonzero).  */
  485. void (*keyboard_init_hook) ();
  486.  
  487. static int read_avail_input ();
  488. static void get_input_pending ();
  489. static int readable_events ();
  490. static Lisp_Object read_char_x_menu_prompt ();
  491. static Lisp_Object read_char_minibuf_menu_prompt ();
  492. static Lisp_Object make_lispy_event ();
  493. static Lisp_Object make_lispy_movement ();
  494. static Lisp_Object modify_event_symbol ();
  495. static Lisp_Object make_lispy_switch_frame ();
  496.  
  497. /* > 0 if we are to echo keystrokes.  */
  498. static int echo_keystrokes;
  499.  
  500. /* Nonzero means echo each character as typed.  */
  501. static int immediate_echo;
  502.  
  503. /* The text we're echoing in the modeline - partial key sequences,
  504.    usually.  '\0'-terminated.  This really shouldn't have a fixed size.  */
  505. static char echobuf[300];
  506.  
  507. /* Where to append more text to echobuf if we want to.  */
  508. static char *echoptr;
  509.  
  510. /* Nonzero means don't try to suspend even if the operating system seems
  511.    to support it.  */
  512. static int cannot_suspend;
  513.  
  514. #define    min(a,b)    ((a)<(b)?(a):(b))
  515. #define    max(a,b)    ((a)>(b)?(a):(b))
  516.  
  517. /* Install the string STR as the beginning of the string of echoing,
  518.    so that it serves as a prompt for the next character.
  519.    Also start echoing.  */
  520.  
  521. echo_prompt (str)
  522.      char *str;
  523. {
  524.   int len = strlen (str);
  525.   if (len > sizeof echobuf - 4)
  526.     len = sizeof echobuf - 4;
  527.   bcopy (str, echobuf, len);
  528.   echoptr = echobuf + len;
  529.   *echoptr = '\0';
  530.  
  531.   echo ();
  532. }
  533.  
  534. /* Add C to the echo string, if echoing is going on.  
  535.    C can be a character, which is printed prettily ("M-C-x" and all that
  536.    jazz), or a symbol, whose name is printed.  */
  537.  
  538. echo_char (c)
  539.      Lisp_Object c;
  540. {
  541.   extern char *push_key_description ();
  542.  
  543.   if (immediate_echo)
  544.     {
  545.       char *ptr = echoptr;
  546.       
  547.       if (ptr != echobuf)
  548.     *ptr++ = ' ';
  549.  
  550.       /* If someone has passed us a composite event, use its head symbol.  */
  551.       c = EVENT_HEAD (c);
  552.  
  553.       if (XTYPE (c) == Lisp_Int)
  554.     {
  555.       if (ptr - echobuf > sizeof echobuf - 6)
  556.         return;
  557.  
  558.       ptr = push_key_description (XINT (c), ptr);
  559.     }
  560.       else if (XTYPE (c) == Lisp_Symbol)
  561.     {
  562.       struct Lisp_String *name = XSYMBOL (c)->name;
  563.       if (((ptr - echobuf) + name->size + 4) > sizeof echobuf)
  564.         return;
  565.       bcopy (name->data, ptr, name->size);
  566.       ptr += name->size;
  567.     }
  568.  
  569.       if (echoptr == echobuf && EQ (c, Vhelp_char))
  570.     {
  571.       strcpy (ptr, " (Type ? for further options)");
  572.       ptr += strlen (ptr);
  573.     }
  574.  
  575.       *ptr = 0;
  576.       echoptr = ptr;
  577.  
  578.       echo ();
  579.     }
  580. }
  581.  
  582. /* Temporarily add a dash to the end of the echo string if it's not
  583.    empty, so that it serves as a mini-prompt for the very next character.  */
  584.  
  585. echo_dash ()
  586. {
  587.   if (!immediate_echo && echoptr == echobuf)
  588.     return;
  589.   /* Do nothing if not echoing at all.  */
  590.   if (echoptr == 0)
  591.     return;
  592.  
  593.   /* Put a dash at the end of the buffer temporarily,
  594.      but make it go away when the next character is added.  */
  595.   echoptr[0] = '-';
  596.   echoptr[1] = 0;
  597.  
  598.   echo ();
  599. }
  600.  
  601. /* Display the current echo string, and begin echoing if not already
  602.    doing so.  */
  603.  
  604. echo ()
  605. {
  606.   if (!immediate_echo)
  607.     {
  608.       int i;
  609.       immediate_echo = 1;
  610.  
  611.       for (i = 0; i < this_command_key_count; i++)
  612.     {
  613.       Lisp_Object c;
  614.       c = XVECTOR (this_command_keys)->contents[i];
  615.       if (! (EVENT_HAS_PARAMETERS (c)
  616.          && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
  617.         echo_char (c);
  618.     }
  619.       echo_dash ();
  620.     }
  621.  
  622.   echoing = 1;
  623.   message1 (echobuf);
  624.   echoing = 0;
  625.  
  626.   if (waiting_for_input && !NILP (Vquit_flag))
  627.     quit_throw_to_read_char ();
  628. }
  629.  
  630. /* Turn off echoing, for the start of a new command.  */
  631.  
  632. cancel_echoing ()
  633. {
  634.   immediate_echo = 0;
  635.   echoptr = echobuf;
  636. }
  637.  
  638. /* Return the length of the current echo string.  */
  639.  
  640. static int
  641. echo_length ()
  642. {
  643.   return echoptr - echobuf;
  644. }
  645.  
  646. /* Truncate the current echo message to its first LEN chars.
  647.    This and echo_char get used by read_key_sequence when the user
  648.    switches frames while entering a key sequence.  */
  649.  
  650. static void
  651. echo_truncate (len)
  652.      int len;
  653. {
  654.   echobuf[len] = '\0';
  655.   echoptr = echobuf + len;
  656.   truncate_echo_area (len);
  657. }
  658.  
  659.  
  660. /* Functions for manipulating this_command_keys.  */
  661. static void
  662. add_command_key (key)
  663.      Lisp_Object key;
  664. {
  665.   int size = XVECTOR (this_command_keys)->size;
  666.  
  667.   if (this_command_key_count >= size)
  668.     {
  669.       Lisp_Object new_keys;
  670.  
  671.       new_keys = Fmake_vector (make_number (size * 2), Qnil);
  672.       bcopy (XVECTOR (this_command_keys)->contents,
  673.          XVECTOR (new_keys)->contents,
  674.          size * sizeof (Lisp_Object));
  675.  
  676.       this_command_keys = new_keys;
  677.     }
  678.  
  679.   XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
  680. }
  681.  
  682. Lisp_Object
  683. recursive_edit_1 ()
  684. {
  685.   int count = specpdl_ptr - specpdl;
  686.   Lisp_Object val;
  687.  
  688.   if (command_loop_level > 0)
  689.     {
  690.       specbind (Qstandard_output, Qt);
  691.       specbind (Qstandard_input, Qt);
  692.     }
  693.  
  694.   val = command_loop ();
  695.   if (EQ (val, Qt))
  696.     Fsignal (Qquit, Qnil);
  697.  
  698.   return unbind_to (count, Qnil);
  699. }
  700.  
  701. /* When an auto-save happens, record the "time", and don't do again soon.  */
  702.  
  703. record_auto_save ()
  704. {
  705.   last_auto_save = num_nonmacro_input_chars;
  706. }
  707.  
  708. /* Make an auto save happen as soon as possible at command level.  */
  709.  
  710. force_auto_save_soon ()
  711. {
  712.   last_auto_save = - auto_save_interval - 1;
  713.  
  714.   record_asynch_buffer_change ();
  715. }
  716.  
  717. DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
  718.   "Invoke the editor command loop recursively.\n\
  719. To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
  720. that tells this function to return.\n\
  721. Alternately, `(throw 'exit t)' makes this function signal an error.\n\
  722. This function is called by the editor initialization to begin editing.")
  723.   ()
  724. {
  725.   int count = specpdl_ptr - specpdl;
  726.   Lisp_Object val;
  727.  
  728.   command_loop_level++;
  729.   update_mode_lines = 1;
  730.  
  731.   record_unwind_protect (recursive_edit_unwind,
  732.              (command_loop_level
  733.               && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
  734.              ? Fcurrent_buffer ()
  735.              : Qnil);
  736.   recursive_edit_1 ();
  737.   return unbind_to (count, Qnil);
  738. }
  739.  
  740. Lisp_Object
  741. recursive_edit_unwind (buffer)
  742.      Lisp_Object buffer;
  743. {
  744.   if (!NILP (buffer))
  745.     Fset_buffer (buffer);
  746.  
  747.   command_loop_level--;
  748.   update_mode_lines = 1;
  749.   return Qnil;
  750. }
  751.  
  752. Lisp_Object
  753. cmd_error (data)
  754.      Lisp_Object data;
  755. {
  756.   Lisp_Object errmsg, tail, errname, file_error;
  757.   Lisp_Object stream;
  758.   struct gcpro gcpro1;
  759.   int i;
  760.  
  761.   Vquit_flag = Qnil;
  762.   Vinhibit_quit = Qt;
  763.   Vstandard_output = Qt;
  764.   Vstandard_input = Qt;
  765.   Vexecuting_macro = Qnil;
  766.   echo_area_glyphs = 0;
  767.  
  768.   /* If the window system or terminal frame hasn't been initialized
  769.      yet, or we're not interactive, it's best to dump this message out
  770.      to stderr and exit.  */
  771.   if (! FRAME_MESSAGE_BUF (selected_frame)
  772.       || noninteractive)
  773.     stream = Qexternal_debugging_output;
  774.   else
  775.     {
  776.       Fdiscard_input ();
  777.       bitch_at_user ();
  778.       stream = Qt;
  779.     }
  780.  
  781.   errname = Fcar (data);
  782.  
  783.   if (EQ (errname, Qerror))
  784.     {
  785.       data = Fcdr (data);
  786.       if (!CONSP (data)) data = Qnil;
  787.       errmsg = Fcar (data);
  788.       file_error = Qnil;
  789.     }
  790.   else
  791.     {
  792.       errmsg = Fget (errname, Qerror_message);
  793.       file_error = Fmemq (Qfile_error,
  794.               Fget (errname, Qerror_conditions));
  795.     }
  796.  
  797.   /* Print an error message including the data items.
  798.      This is done by printing it into a scratch buffer
  799.      and then making a copy of the text in the buffer. */
  800.  
  801.   if (!CONSP (data)) data = Qnil;
  802.   tail = Fcdr (data);
  803.   GCPRO1 (tail);
  804.  
  805.   /* For file-error, make error message by concatenating
  806.      all the data items.  They are all strings.  */
  807.   if (!NILP (file_error) && !NILP (tail))
  808.     errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
  809.  
  810.   if (XTYPE (errmsg) == Lisp_String)
  811.     Fprinc (errmsg, stream);
  812.   else
  813.     write_string_1 ("peculiar error", -1, stream);
  814.  
  815.   for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
  816.     {
  817.       write_string_1 (i ? ", " : ": ", 2, stream);
  818.       if (!NILP (file_error))
  819.     Fprinc (Fcar (tail), stream);
  820.       else
  821.     Fprin1 (Fcar (tail), stream);
  822.     }
  823.   UNGCPRO;
  824.  
  825.   /* If the window system or terminal frame hasn't been initialized
  826.      yet, or we're in -batch mode, this error should cause Emacs to exit.  */
  827.   if (! FRAME_MESSAGE_BUF (selected_frame)
  828.       || noninteractive)
  829.     {
  830.       Fterpri (stream);
  831.       Fkill_emacs (make_number (-1));
  832.     }
  833.  
  834.   Vquit_flag = Qnil;
  835.  
  836.   Vinhibit_quit = Qnil;
  837.   return make_number (0);
  838. }
  839.  
  840. Lisp_Object command_loop_1 ();
  841. Lisp_Object command_loop_2 ();
  842. Lisp_Object top_level_1 ();
  843.  
  844. /* Entry to editor-command-loop.
  845.    This level has the catches for exiting/returning to editor command loop.
  846.    It returns nil to exit recursive edit, t to abort it.  */
  847.  
  848. Lisp_Object
  849. command_loop ()
  850. {
  851.   if (command_loop_level > 0 || minibuf_level > 0)
  852.     {
  853.       return internal_catch (Qexit, command_loop_2, Qnil);
  854.     }
  855.   else
  856.     while (1)
  857.       {
  858.     internal_catch (Qtop_level, top_level_1, Qnil);
  859.     internal_catch (Qtop_level, command_loop_2, Qnil);
  860.     
  861.     /* End of file in -batch run causes exit here.  */
  862.     if (noninteractive)
  863.       Fkill_emacs (Qt);
  864.       }
  865. }
  866.  
  867. /* Here we catch errors in execution of commands within the
  868.    editing loop, and reenter the editing loop.
  869.    When there is an error, cmd_error runs and returns a non-nil
  870.    value to us.  A value of nil means that cmd_loop_1 itself
  871.    returned due to end of file (or end of kbd macro).  */
  872.  
  873. Lisp_Object
  874. command_loop_2 ()
  875. {
  876.   register Lisp_Object val;
  877.  
  878.   do
  879.     val = internal_condition_case (command_loop_1, Qerror, cmd_error);
  880.   while (!NILP (val));
  881.  
  882.   return Qnil;
  883. }
  884.  
  885. Lisp_Object
  886. top_level_2 ()
  887. {
  888.   return Feval (Vtop_level);
  889. }
  890.  
  891. Lisp_Object
  892. top_level_1 ()
  893. {
  894.   /* On entry to the outer level, run the startup file */
  895.   if (!NILP (Vtop_level))
  896.     internal_condition_case (top_level_2, Qerror, cmd_error);
  897.   else if (!NILP (Vpurify_flag))
  898.     message ("Bare impure Emacs (standard Lisp code not loaded)");
  899.   else
  900.     message ("Bare Emacs (standard Lisp code not loaded)");
  901.   return Qnil;
  902. }
  903.  
  904. DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
  905.   "Exit all recursive editing levels.")
  906.   ()
  907. {
  908.   Fthrow (Qtop_level, Qnil);
  909. }
  910.  
  911. DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
  912.   "Exit from the innermost recursive edit or minibuffer.")
  913.   ()
  914. {
  915.   if (command_loop_level > 0 || minibuf_level > 0)
  916.     Fthrow (Qexit, Qnil);
  917.  
  918.   error ("No recursive edit is in progress");
  919. }
  920.  
  921. DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
  922.   "Abort the command that requested this recursive edit or minibuffer input.")
  923.   ()
  924. {
  925.   if (command_loop_level > 0 || minibuf_level > 0)
  926.     Fthrow (Qexit, Qt);
  927.  
  928.   error ("No recursive edit is in progress");
  929. }
  930.  
  931. /* This is the actual command reading loop,
  932.    sans error-handling encapsulation.  */
  933.  
  934. Lisp_Object Fcommand_execute ();
  935. static int read_key_sequence ();
  936. static void safe_run_hooks ();
  937.  
  938. Lisp_Object
  939. command_loop_1 ()
  940. {
  941.   Lisp_Object cmd, tem;
  942.   int lose;
  943.   int nonundocount;
  944.   Lisp_Object keybuf[30];
  945.   int i;
  946.   int no_redisplay;
  947.   int no_direct;
  948.   int prev_modiff;
  949.   struct buffer *prev_buffer;
  950.  
  951.   Vprefix_arg = Qnil;
  952.   Vdeactivate_mark = Qnil;
  953.   waiting_for_input = 0;
  954.   cancel_echoing ();
  955.  
  956.   nonundocount = 0;
  957.   no_redisplay = 0;
  958.   this_command_key_count = 0;
  959.  
  960.   /* Make sure this hook runs after commands that get errors and
  961.      throw to top level.  */
  962.   if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
  963.     safe_run_hooks (&Vpost_command_hook);
  964.  
  965.   /* Do this after running Vpost_command_hook, for consistency.  */
  966.   last_command = this_command;
  967.  
  968.   while (1)
  969.     {
  970.       /* Install chars successfully executed in kbd macro.  */
  971.  
  972.       if (defining_kbd_macro && NILP (Vprefix_arg))
  973.     finalize_kbd_macro_chars ();
  974.  
  975.       /* Make sure the current window's buffer is selected.  */
  976.       if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  977.     set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
  978.  
  979.       /* Display any malloc warning that just came out.  Use while because
  980.      displaying one warning can cause another.  */
  981.  
  982.       while (pending_malloc_warning)
  983.     display_malloc_warning ();
  984.  
  985.       no_direct = 0;
  986.  
  987.       Vdeactivate_mark = Qnil;
  988.  
  989.       /* If minibuffer on and echo area in use,
  990.      wait 2 sec and redraw minibuffer.  */
  991.  
  992.       if (minibuf_level && echo_area_glyphs)
  993.     {
  994.       /* Bind inhibit-quit to t so that C-g gets read in
  995.          rather than quitting back to the minibuffer.  */
  996.       int count = specpdl_ptr - specpdl;
  997.       specbind (Qinhibit_quit, Qt);
  998.       Fsit_for (make_number (2), Qnil, Qnil);
  999.       unbind_to (count, Qnil);
  1000.  
  1001.       echo_area_glyphs = 0;
  1002.       no_direct = 1;
  1003.       if (!NILP (Vquit_flag))
  1004.         {
  1005.           Vquit_flag = Qnil;
  1006.           Vunread_command_events = Fcons (make_number (quit_char), Qnil);
  1007.         }
  1008.     }
  1009.  
  1010. #ifdef C_ALLOCA
  1011.       alloca (0);        /* Cause a garbage collection now */
  1012.                 /* Since we can free the most stuff here.  */
  1013. #endif /* C_ALLOCA */
  1014.  
  1015. #if 0
  1016. #ifdef MULTI_FRAME
  1017.       /* Select the frame that the last event came from.  Usually,
  1018.      switch-frame events will take care of this, but if some lisp
  1019.      code swallows a switch-frame event, we'll fix things up here.
  1020.      Is this a good idea?  */
  1021.       if (XTYPE (internal_last_event_frame) == Lisp_Frame
  1022.       && XFRAME (internal_last_event_frame) != selected_frame)
  1023.     Fselect_frame (internal_last_event_frame, Qnil);
  1024. #endif
  1025. #endif
  1026.       /* If it has changed current-menubar from previous value,
  1027.      really recompute the menubar from the value.  */
  1028.       if (! NILP (Vlucid_menu_bar_dirty_flag)
  1029.       && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
  1030.     call0 (Qrecompute_lucid_menubar);
  1031.  
  1032.       /* Read next key sequence; i gets its length.  */
  1033.       i = read_key_sequence (keybuf, (sizeof keybuf / sizeof (keybuf[0])), Qnil);
  1034.  
  1035.       ++num_input_keys;
  1036.  
  1037.       /* Now we have read a key sequence of length I,
  1038.      or else I is 0 and we found end of file.  */
  1039.  
  1040.       if (i == 0)        /* End of file -- happens only in */
  1041.     return Qnil;        /* a kbd macro, at the end.  */
  1042.       /* -1 means read_key_sequence got a menu that was rejected.
  1043.      Just loop around and read another command.  */
  1044.       if (i == -1)
  1045.     {
  1046.       cancel_echoing ();
  1047.       this_command_key_count = 0;
  1048.       continue;
  1049.     }
  1050.  
  1051.       last_command_char = keybuf[i - 1];
  1052.  
  1053.       /* If the previous command tried to force a specific window-start,
  1054.      forget about that, in case this command moves point far away
  1055.      from that position.  */
  1056.       XWINDOW (selected_window)->force_start = Qnil;
  1057.  
  1058.       cmd = read_key_sequence_cmd;
  1059.       if (!NILP (Vexecuting_macro))
  1060.     {
  1061.       if (!NILP (Vquit_flag))
  1062.         {
  1063.           Vexecuting_macro = Qt;
  1064.           QUIT;        /* Make some noise. */
  1065.                 /* Will return since macro now empty. */
  1066.         }
  1067.     }
  1068.  
  1069.       /* Do redisplay processing after this command except in special
  1070.      cases identified below that set no_redisplay to 1.
  1071.      (actually, there's currently no way to prevent the redisplay,
  1072.      and no_redisplay is ignored.
  1073.      Perhaps someday we will really implement it.  */
  1074.       no_redisplay = 0;
  1075.  
  1076.       prev_buffer = current_buffer;
  1077.       prev_modiff = MODIFF;
  1078.       last_point_position = PT;
  1079.       XSET (last_point_position_buffer, Lisp_Buffer, prev_buffer);
  1080.  
  1081.       /* Execute the command.  */
  1082.  
  1083.       this_command = cmd;
  1084.       if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
  1085.     safe_run_hooks (&Vpre_command_hook);
  1086.  
  1087.       if (NILP (this_command))
  1088.     {
  1089.       /* nil means key is undefined.  */
  1090.       bitch_at_user ();
  1091.       defining_kbd_macro = 0;
  1092.       update_mode_lines = 1;
  1093.       Vprefix_arg = Qnil;
  1094.  
  1095.     }
  1096.       else
  1097.     {
  1098.       if (NILP (Vprefix_arg) && ! no_direct)
  1099.         {
  1100.           /* Recognize some common commands in common situations and
  1101.          do them directly.  */
  1102.           if (EQ (this_command, Qforward_char) && PT < ZV)
  1103.         {
  1104.                   struct Lisp_Vector *dp
  1105.             = window_display_table (XWINDOW (selected_window));
  1106.           lose = FETCH_CHAR (PT);
  1107.           SET_PT (PT + 1);
  1108.           if ((dp
  1109.                ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
  1110.               && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
  1111.                : (lose >= 0x20 && lose < 0x7f))
  1112.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  1113.               >= MODIFF)
  1114.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  1115.               == PT - 1)
  1116.               && !windows_or_buffers_changed
  1117.               && EQ (current_buffer->selective_display, Qnil)
  1118.               && !detect_input_pending ()
  1119.               && NILP (Vexecuting_macro))
  1120.             no_redisplay = direct_output_forward_char (1);
  1121.           goto directly_done;
  1122.         }
  1123.           else if (EQ (this_command, Qbackward_char) && PT > BEGV)
  1124.         {
  1125.                   struct Lisp_Vector *dp
  1126.             = window_display_table (XWINDOW (selected_window));
  1127.           SET_PT (PT - 1);
  1128.           lose = FETCH_CHAR (PT);
  1129.           if ((dp
  1130.                ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
  1131.               && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
  1132.                : (lose >= 0x20 && lose < 0x7f))
  1133.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  1134.               >= MODIFF)
  1135.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  1136.               == PT + 1)
  1137.               && !windows_or_buffers_changed
  1138.               && EQ (current_buffer->selective_display, Qnil)
  1139.               && !detect_input_pending ()
  1140.               && NILP (Vexecuting_macro))
  1141.             no_redisplay = direct_output_forward_char (-1);
  1142.           goto directly_done;
  1143.         }
  1144.           else if (EQ (this_command, Qself_insert_command)
  1145.                /* Try this optimization only on ascii keystrokes.  */
  1146.                && XTYPE (last_command_char) == Lisp_Int)
  1147.         {
  1148.           unsigned char c = XINT (last_command_char);
  1149.  
  1150.           if (NILP (Vexecuting_macro) &&
  1151.               !EQ (minibuf_window, selected_window))
  1152.             {
  1153.               if (!nonundocount || nonundocount >= 20)
  1154.             {
  1155.               Fundo_boundary ();
  1156.               nonundocount = 0;
  1157.             }
  1158.               nonundocount++;
  1159.             }
  1160.           lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
  1161.               < MODIFF)
  1162.             || (XFASTINT (XWINDOW (selected_window)->last_point) != PT)
  1163.             || MODIFF <= current_buffer->save_modified
  1164.             || windows_or_buffers_changed
  1165.             || !EQ (current_buffer->selective_display, Qnil)
  1166.             || detect_input_pending ()
  1167.             || !NILP (Vexecuting_macro);
  1168.           if (internal_self_insert (c, 0))
  1169.             {
  1170.               lose = 1;
  1171.               nonundocount = 0;
  1172.             }
  1173.           if (!lose &&
  1174.               (PT == ZV || FETCH_CHAR (PT) == '\n'))
  1175.             {
  1176.               struct Lisp_Vector *dp
  1177.             = window_display_table (XWINDOW (selected_window));
  1178.               int lose = c;
  1179.               Lisp_Object obj;
  1180.  
  1181.               if (dp && !NILP ((obj = DISP_CHAR_VECTOR (dp, lose))))
  1182.             {
  1183.               if (XTYPE (obj) == Lisp_Vector
  1184.                   && XVECTOR (obj)->size == 1
  1185.                   && (XTYPE (obj = XVECTOR (obj)->contents[0])
  1186.                       == Lisp_Int)
  1187.                   /* Insist face not specified in glyph.  */
  1188.                   && (XINT (obj) & ((-1) << 8)) == 0)
  1189.                 no_redisplay
  1190.                   = direct_output_for_insert (XINT (obj));
  1191.             }
  1192.               else
  1193.             {
  1194.               if (lose >= 0x20 && lose <= 0x7e)
  1195.                 no_redisplay = direct_output_for_insert (lose);
  1196.             }
  1197.             }
  1198.           goto directly_done;
  1199.         }
  1200.         }
  1201.  
  1202.       /* Here for a command that isn't executed directly */
  1203.  
  1204.       nonundocount = 0;
  1205.       if (NILP (Vprefix_arg))
  1206.         Fundo_boundary ();
  1207.       Fcommand_execute (this_command, Qnil);
  1208.  
  1209.     }
  1210.     directly_done: ;
  1211.  
  1212.       if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
  1213.     safe_run_hooks (&Vpost_command_hook);
  1214.  
  1215.       /* If there is a prefix argument,
  1216.      1) We don't want last_command to be ``universal-argument''
  1217.      (that would be dumb), so don't set last_command,
  1218.      2) we want to leave echoing on so that the prefix will be
  1219.      echoed as part of this key sequence, so don't call
  1220.      cancel_echoing, and
  1221.      3) we want to leave this_command_key_count non-zero, so that
  1222.      read_char will realize that it is re-reading a character, and
  1223.      not echo it a second time.  */
  1224.       if (NILP (Vprefix_arg))
  1225.     {
  1226.       last_command = this_command;
  1227.       cancel_echoing ();
  1228.       this_command_key_count = 0;
  1229.     }
  1230.  
  1231.       if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
  1232.     {
  1233.       if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
  1234.         {
  1235.           current_buffer->mark_active = Qnil;
  1236.           call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
  1237.         }
  1238.       else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
  1239.         call1 (Vrun_hooks, intern ("activate-mark-hook"));
  1240.     }
  1241.     }
  1242. }
  1243.  
  1244. /* If we get an error while running the hook, cause the hook variable
  1245.    to be nil.  Also inhibit quits, so that C-g won't cause the hook
  1246.    to mysteriously evaporate.  */
  1247. static void
  1248. safe_run_hooks (hook)
  1249.      Lisp_Object *hook;
  1250. {
  1251.   int count = specpdl_ptr - specpdl;
  1252.   specbind (Qinhibit_quit, Qt);
  1253.  
  1254.   Vcommand_hook_internal = *hook;
  1255.   *hook = Qnil;
  1256.   call1 (Vrun_hooks, Qcommand_hook_internal);
  1257.   *hook = Vcommand_hook_internal;
  1258.  
  1259.   unbind_to (count, Qnil);
  1260. }
  1261.  
  1262. /* Number of seconds between polling for input.  */
  1263. int polling_period;
  1264.  
  1265. /* Nonzero means polling for input is temporarily suppressed.  */
  1266. int poll_suppress_count;
  1267.  
  1268. #ifdef POLL_FOR_INPUT
  1269. int polling_for_input;
  1270.  
  1271. /* Handle an alarm once each second and read pending input
  1272.    so as to handle a C-g if it comces in.  */
  1273.  
  1274. SIGTYPE
  1275. input_poll_signal ()
  1276. {
  1277.   if (interrupt_input_blocked == 0
  1278.       && !waiting_for_input)
  1279.     read_avail_input (0);
  1280.   signal (SIGALRM, input_poll_signal);
  1281.   alarm (polling_period);
  1282. }
  1283.  
  1284. #endif
  1285.  
  1286. /* Begin signals to poll for input, if they are appropriate.
  1287.    This function is called unconditionally from various places.  */
  1288.  
  1289. start_polling ()
  1290. {
  1291. #ifdef POLL_FOR_INPUT
  1292.   if (read_socket_hook && !interrupt_input)
  1293.     {
  1294.       poll_suppress_count--;
  1295.       if (poll_suppress_count == 0)
  1296.     {
  1297.       signal (SIGALRM, input_poll_signal);
  1298.       polling_for_input = 1;
  1299.       alarm (polling_period);
  1300.     }
  1301.     }
  1302. #endif
  1303. }
  1304.  
  1305. /* Turn off polling.  */
  1306.  
  1307. stop_polling ()
  1308. {
  1309. #ifdef POLL_FOR_INPUT
  1310.   if (read_socket_hook && !interrupt_input)
  1311.     {
  1312.       if (poll_suppress_count == 0)
  1313.     {
  1314.       polling_for_input = 0;
  1315.       alarm (0);
  1316.     }
  1317.       poll_suppress_count++;
  1318.     }
  1319. #endif
  1320. }
  1321.  
  1322. /* Set the value of poll_suppress_count to COUNT
  1323.    and start or stop polling accordingly.  */
  1324.  
  1325. void
  1326. set_poll_suppress_count (count)
  1327.      int count;
  1328. {
  1329. #ifdef POLL_FOR_INPUT
  1330.   if (count == 0 && poll_suppress_count != 0)
  1331.     {
  1332.       poll_suppress_count = 1;
  1333.       start_polling ();
  1334.     }
  1335.   else if (count != 0 && poll_suppress_count == 0)
  1336.     {
  1337.       stop_polling ();
  1338.     }
  1339.   poll_suppress_count = count;
  1340. #endif
  1341. }
  1342.  
  1343. /* Bind polling_period to a value at least N.
  1344.    But don't decrease it.  */
  1345.  
  1346. bind_polling_period (n)
  1347.      int n;
  1348. {
  1349. #ifdef POLL_FOR_INPUT
  1350.   int new = polling_period;
  1351.  
  1352.   if (n > new)
  1353.     new = n;
  1354.  
  1355.   stop_polling ();
  1356.   specbind (Qpolling_period, make_number (new));
  1357.   /* Start a new alarm with the new period.  */
  1358.   start_polling ();
  1359. #endif
  1360. }
  1361.  
  1362. /* Applying the control modifier to CHARACTER.  */
  1363. int
  1364. make_ctrl_char (c)
  1365.      int c;
  1366. {
  1367.   /* Save the upper bits here.  */
  1368.   int upper = c & ~0177;
  1369.  
  1370.   c &= 0177;
  1371.  
  1372.   /* Everything in the columns containing the upper-case letters
  1373.      denotes a control character.  */
  1374.   if (c >= 0100 && c < 0140)
  1375.     {
  1376.       int oc = c;
  1377.       c &= ~0140;
  1378.       /* Set the shift modifier for a control char
  1379.      made from a shifted letter.  But only for letters!  */
  1380.       if (oc >= 'A' && oc <= 'Z')
  1381.     c |= shift_modifier;
  1382.     }
  1383.  
  1384.   /* The lower-case letters denote control characters too.  */
  1385.   else if (c >= 'a' && c <= 'z')
  1386.     c &= ~0140;
  1387.  
  1388.   /* Include the bits for control and shift
  1389.      only if the basic ASCII code can't indicate them.  */
  1390.   else if (c >= ' ')
  1391.     c |= ctrl_modifier;
  1392.  
  1393.   /* Replace the high bits.  */
  1394.   c |= (upper & ~ctrl_modifier);
  1395.  
  1396.   return c;
  1397. }
  1398.  
  1399.  
  1400.  
  1401. /* Input of single characters from keyboard */
  1402.  
  1403. Lisp_Object print_help ();
  1404. static Lisp_Object kbd_buffer_get_event ();
  1405.  
  1406. /* read a character from the keyboard; call the redisplay if needed */
  1407. /* commandflag 0 means do not do auto-saving, but do do redisplay.
  1408.    -1 means do not do redisplay, but do do autosaving.
  1409.    1 means do both.  */
  1410.  
  1411. /* The arguments MAPS and NMAPS are for menu prompting.
  1412.    MAPS is an array of keymaps;  NMAPS is the length of MAPS.
  1413.  
  1414.    PREV_EVENT is the previous input event, or nil if we are reading
  1415.    the first event of a key sequence.
  1416.  
  1417.    If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
  1418.    if we used a mouse menu to read the input, or zero otherwise.  If
  1419.    USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
  1420.  
  1421.    Value is t if we showed a menu and the user rejected it.  */
  1422.  
  1423. Lisp_Object
  1424. read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
  1425.      int commandflag;
  1426.      int nmaps;
  1427.      Lisp_Object *maps;
  1428.      Lisp_Object prev_event;
  1429.      int *used_mouse_menu;
  1430. {
  1431.   register Lisp_Object c;
  1432.   int count;
  1433.   jmp_buf save_jump;
  1434.  
  1435.   if (CONSP (Vunread_command_events))
  1436.     {
  1437.       c = XCONS (Vunread_command_events)->car;
  1438.       Vunread_command_events = XCONS (Vunread_command_events)->cdr;
  1439.  
  1440.       if (this_command_key_count == 0)
  1441.     goto reread_first;
  1442.       else
  1443.     goto reread;
  1444.     }
  1445.  
  1446.   if (unread_command_char != -1)
  1447.     {
  1448.       XSET (c, Lisp_Int, unread_command_char);
  1449.       unread_command_char = -1;
  1450.  
  1451.       if (this_command_key_count == 0)
  1452.     goto reread_first;
  1453.       else
  1454.     goto reread;
  1455.     }
  1456.  
  1457.   if (!NILP (Vexecuting_macro))
  1458.     {
  1459. #ifdef MULTI_FRAME
  1460.       /* We set this to Qmacro; since that's not a frame, nobody will
  1461.      try to switch frames on us, and the selected window will
  1462.      remain unchanged.
  1463.  
  1464.          Since this event came from a macro, it would be misleading to
  1465.      leave internal_last_event_frame set to wherever the last
  1466.      real event came from.  Normally, a switch-frame event selects
  1467.      internal_last_event_frame after each command is read, but
  1468.      events read from a macro should never cause a new frame to be
  1469.      selected. */
  1470.       Vlast_event_frame = internal_last_event_frame = Qmacro;
  1471. #endif
  1472.  
  1473.       /* Exit the macro if we are at the end.
  1474.      Also, some things replace the macro with t
  1475.      to force an early exit.  */
  1476.       if (EQ (Vexecuting_macro, Qt)
  1477.       || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
  1478.     {
  1479.       XSET (c, Lisp_Int, -1);
  1480.       return c;
  1481.     }
  1482.       
  1483.       c = Faref (Vexecuting_macro, make_number (executing_macro_index));
  1484.       if (XTYPE (Vexecuting_macro) == Lisp_String
  1485.       && (XINT (c) & 0x80))
  1486.     XFASTINT (c) = CHAR_META | (XINT (c) & ~0x80);
  1487.  
  1488.       executing_macro_index++;
  1489.  
  1490.       goto from_macro;
  1491.     }
  1492.  
  1493.   if (!NILP (unread_switch_frame))
  1494.     {
  1495.       c = unread_switch_frame;
  1496.       unread_switch_frame = Qnil;
  1497.  
  1498.       /* This event should make it into this_command_keys, and get echoed
  1499.      again, so we go to reread_first, rather than reread.  */
  1500.       goto reread_first;
  1501.     }
  1502.  
  1503.   /* Don't bother updating menu bars while doing mouse tracking.
  1504.      We get events very rapidly then, and the menu bar won't be changing.
  1505.      We do update the menu bar once on entry to Ftrack_mouse.  */
  1506.   if (commandflag > 0 && !input_pending && !detect_input_pending ())
  1507.     prepare_menu_bars ();
  1508.  
  1509.   /* Save outer setjmp data, in case called recursively.  */
  1510.   save_getcjmp (save_jump);
  1511.  
  1512.   stop_polling ();
  1513.  
  1514.   if (commandflag >= 0 && !input_pending && !detect_input_pending ())
  1515.     redisplay ();
  1516.  
  1517.   if (_setjmp (getcjmp))
  1518.     {
  1519.       XSET (c, Lisp_Int, quit_char);
  1520. #ifdef MULTI_FRAME
  1521.       XSET (internal_last_event_frame, Lisp_Frame, selected_frame);
  1522.       Vlast_event_frame = internal_last_event_frame;
  1523. #endif
  1524.       /* If we report the quit char as an event,
  1525.      don't do so more than once.  */
  1526.       if (!NILP (Vinhibit_quit))
  1527.     Vquit_flag = Qnil;
  1528.  
  1529.       goto non_reread;
  1530.     }
  1531.  
  1532.   /* Message turns off echoing unless more keystrokes turn it on again. */
  1533.   if (echo_area_glyphs && *echo_area_glyphs && echo_area_glyphs != echobuf)
  1534.     cancel_echoing ();
  1535.   else
  1536.     /* If already echoing, continue.  */
  1537.     echo_dash ();
  1538.  
  1539.   /* Try reading a character via menu prompting in the minibuf.
  1540.      Try this before the sit-for, because the sit-for
  1541.      would do the wrong thing if we are supposed to do
  1542.      menu prompting. If EVENT_HAS_PARAMETERS then we are reading
  1543.      after a mouse event so don't try a minibuf menu. */
  1544.   c = Qnil;
  1545.   if (nmaps > 0 && INTERACTIVE
  1546.       && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
  1547.       /* Don't bring up a menu if we already have another event.  */
  1548.       && NILP (Vunread_command_events)
  1549.       && unread_command_char < 0
  1550.       && !detect_input_pending ())
  1551.     {
  1552.       c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
  1553.       if (! NILP (c))
  1554.     return c;
  1555.     }
  1556.  
  1557.   /* If in middle of key sequence and minibuffer not active,
  1558.      start echoing if enough time elapses.  */
  1559.   if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
  1560.       && ! noninteractive
  1561.       && echo_keystrokes > 0
  1562.       && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
  1563.     {
  1564.       Lisp_Object tem0;
  1565.  
  1566.       /* After a mouse event, start echoing right away.
  1567.      This is because we are probably about to display a menu,
  1568.      and we don't want to delay before doing so.  */
  1569.       if (EVENT_HAS_PARAMETERS (prev_event))
  1570.     echo ();
  1571.       else
  1572.     {
  1573.       tem0 = sit_for (echo_keystrokes, 0, 1, 1);
  1574.       if (EQ (tem0, Qt))
  1575.         echo ();
  1576.     }
  1577.     }
  1578.  
  1579.   /* Maybe auto save due to number of keystrokes or idle time.  */
  1580.  
  1581.   if (commandflag != 0
  1582.       && auto_save_interval > 0
  1583.       && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
  1584.       && !detect_input_pending ())
  1585.     {
  1586.       jmp_buf temp;
  1587.       save_getcjmp (temp);
  1588.       Fdo_auto_save (Qnil, Qnil);
  1589.       restore_getcjmp (temp);
  1590.     }
  1591.  
  1592.   /* Try reading using an X menu.
  1593.      This is never confused with reading using the minibuf
  1594.      because the recursive call of read_char in read_char_minibuf_menu_prompt
  1595.      does not pass on any keymaps.  */
  1596.   if (nmaps > 0 && INTERACTIVE
  1597.       && !NILP (prev_event) && EVENT_HAS_PARAMETERS (prev_event)
  1598.       /* Don't bring up a menu if we already have another event.  */
  1599.       && NILP (Vunread_command_events)
  1600.       && unread_command_char < 0)
  1601.     c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
  1602.  
  1603.   /* Slow down auto saves logarithmically in size of current buffer,
  1604.      and garbage collect while we're at it.  */
  1605.   if (INTERACTIVE && NILP (c))
  1606.     {
  1607.       int delay_level, buffer_size;
  1608.  
  1609.       if (! MINI_WINDOW_P (XWINDOW (selected_window)))
  1610.     last_non_minibuf_size = Z - BEG;
  1611.       buffer_size = (last_non_minibuf_size >> 8) + 1;
  1612.       delay_level = 0;
  1613.       while (buffer_size > 64)
  1614.     delay_level++, buffer_size -= buffer_size >> 2;
  1615.       if (delay_level < 4) delay_level = 4;
  1616.       /* delay_level is 4 for files under around 50k, 7 at 100k,
  1617.      9 at 200k, 11 at 300k, and 12 at 500k.  It is 15 at 1 meg.  */
  1618.  
  1619.       /* Auto save if enough time goes by without input.  */
  1620.       if (commandflag != 0
  1621.       && num_nonmacro_input_chars > last_auto_save
  1622.       && XTYPE (Vauto_save_timeout) == Lisp_Int
  1623.       && XINT (Vauto_save_timeout) > 0)
  1624.     {
  1625.       Lisp_Object tem0;
  1626.       int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
  1627.       tem0 = sit_for (delay, 0, 1, 1);
  1628.       if (EQ (tem0, Qt))
  1629.         {
  1630.           jmp_buf temp;
  1631.           save_getcjmp (temp);
  1632.           Fdo_auto_save (Qnil, Qnil);
  1633.           restore_getcjmp (temp);
  1634.  
  1635.           /* If we have auto-saved and there is still no input
  1636.          available, garbage collect if there has been enough
  1637.          consing going on to make it worthwhile.  */
  1638.           if (!detect_input_pending ()
  1639.           && consing_since_gc > gc_cons_threshold / 2)
  1640.         {
  1641.           Fgarbage_collect ();
  1642.           /* prepare_menu_bars isn't safe here, but it should
  1643.              also be unnecessary.  */
  1644.           redisplay ();
  1645.         }
  1646.         }
  1647.     }
  1648.     }
  1649.  
  1650.   /* Actually read a character, waiting if necessary.  */
  1651.   while (NILP (c))
  1652.     {
  1653.       c = kbd_buffer_get_event ();
  1654.       if (!NILP (c))
  1655.     break;
  1656.       if (commandflag >= 0 && !input_pending && !detect_input_pending ())
  1657.     redisplay ();
  1658.     }
  1659.  
  1660.   /* Terminate Emacs in batch mode if at eof.  */
  1661.   if (noninteractive && XTYPE (c) == Lisp_Int && XINT (c) < 0)
  1662.     Fkill_emacs (make_number (1));
  1663.  
  1664.   if (XTYPE (c) == Lisp_Int)
  1665.     {
  1666.       /* Add in any extra modifiers, where appropriate.  */
  1667.       if ((extra_keyboard_modifiers & CHAR_CTL)
  1668.       || ((extra_keyboard_modifiers & 0177) < ' '
  1669.           && (extra_keyboard_modifiers & 0177) != 0))
  1670.     XSETINT (c, make_ctrl_char (XINT (c)));
  1671.  
  1672.       /* Transfer any other modifier bits directly from
  1673.      extra_keyboard_modifiers to c.  Ignore the actual character code
  1674.      in the low 16 bits of extra_keyboard_modifiers.  */
  1675.       XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
  1676.     }
  1677.  
  1678.  non_reread:
  1679.  
  1680.   restore_getcjmp (save_jump);
  1681.  
  1682.   start_polling ();
  1683.  
  1684.   /* Don't wipe the echo area for a trivial event.  */
  1685.   if (XTYPE (c) != Lisp_Buffer)
  1686.     echo_area_glyphs = 0;
  1687.  
  1688.   /* Handle things that only apply to characters.  */
  1689.   if (XTYPE (c) == Lisp_Int)
  1690.     {
  1691.       /* If kbd_buffer_get_event gave us an EOF, return that.  */
  1692.       if (XINT (c) == -1)
  1693.     return c;
  1694.  
  1695.       if (XTYPE (Vkeyboard_translate_table) == Lisp_String
  1696.       && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
  1697.     XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
  1698.     }
  1699.  
  1700.   total_keys++;
  1701.   XVECTOR (recent_keys)->contents[recent_keys_index] = c;
  1702.   if (++recent_keys_index >= NUM_RECENT_KEYS)
  1703.     recent_keys_index = 0;
  1704.  
  1705.   /* Write c to the dribble file.  If c is a lispy event, write
  1706.      the event's symbol to the dribble file, in <brackets>.  Bleaugh.
  1707.      If you, dear reader, have a better idea, you've got the source.  :-) */
  1708.   if (dribble)
  1709.     {
  1710.       if (XTYPE (c) == Lisp_Int)
  1711.     putc (XINT (c), dribble);
  1712.       else
  1713.     {
  1714.       Lisp_Object dribblee;
  1715.  
  1716.       /* If it's a structured event, take the event header.  */
  1717.       dribblee = EVENT_HEAD (c);
  1718.  
  1719.       if (XTYPE (dribblee) == Lisp_Symbol)
  1720.         {
  1721.           putc ('<', dribble);
  1722.           fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
  1723.               XSYMBOL (dribblee)->name->size,
  1724.               dribble);
  1725.           putc ('>', dribble);
  1726.         }
  1727.     }
  1728.  
  1729.       fflush (dribble);
  1730.     }
  1731.  
  1732.   store_kbd_macro_char (c);
  1733.  
  1734.   num_nonmacro_input_chars++;
  1735.  
  1736.  from_macro:
  1737.  reread_first:
  1738.  
  1739.   /* Don't echo mouse motion events.  */
  1740.   if (! (EVENT_HAS_PARAMETERS (c)
  1741.      && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
  1742.     echo_char (c);
  1743.  
  1744.   /* Record this character as part of the current key.  */
  1745.   add_command_key (c);
  1746.  
  1747.   /* Re-reading in the middle of a command */
  1748.  reread:
  1749.   last_input_char = c;
  1750.   num_input_chars++;
  1751.  
  1752.   /* Process the help character specially if enabled */
  1753.   if (EQ (c, Vhelp_char) && !NILP (Vhelp_form))
  1754.     {
  1755.       Lisp_Object tem0;
  1756.       count = specpdl_ptr - specpdl;
  1757.  
  1758.       record_unwind_protect (Fset_window_configuration,
  1759.                  Fcurrent_window_configuration (Qnil));
  1760.  
  1761.       tem0 = Feval (Vhelp_form);
  1762.       if (XTYPE (tem0) == Lisp_String)
  1763.     internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
  1764.  
  1765.       cancel_echoing ();
  1766.       do
  1767.     c = read_char (0, 0, 0, Qnil, 0);
  1768.       while (XTYPE (c) == Lisp_Buffer);
  1769.       /* Remove the help from the frame */
  1770.       unbind_to (count, Qnil);
  1771.       prepare_menu_bars ();
  1772.       redisplay ();
  1773.       if (EQ (c, make_number (040)))
  1774.     {
  1775.       cancel_echoing ();
  1776.       do
  1777.         c = read_char (0, 0, 0, Qnil, 0);
  1778.       while (XTYPE (c) == Lisp_Buffer);
  1779.     }
  1780.     }
  1781.  
  1782.   return c;
  1783. }
  1784.  
  1785. Lisp_Object
  1786. print_help (object)
  1787.      Lisp_Object object;
  1788. {
  1789.   Fprinc (object, Qnil);
  1790.   return Qnil;
  1791. }
  1792.  
  1793. /* Copy out or in the info on where C-g should throw to.
  1794.    This is used when running Lisp code from within get_char,
  1795.    in case get_char is called recursively.
  1796.    See read_process_output.  */
  1797.  
  1798. save_getcjmp (temp)
  1799.      jmp_buf temp;
  1800. {
  1801.   bcopy (getcjmp, temp, sizeof getcjmp);
  1802. }
  1803.  
  1804. restore_getcjmp (temp)
  1805.      jmp_buf temp;
  1806. {
  1807.   bcopy (temp, getcjmp, sizeof getcjmp);
  1808. }
  1809.  
  1810.  
  1811. /* Restore mouse tracking enablement.  See Ftrack_mouse for the only use
  1812.    of this function.  */
  1813. static Lisp_Object
  1814. tracking_off (old_value)
  1815.      Lisp_Object old_value;
  1816. {
  1817.   if (! XFASTINT (old_value))
  1818.     {
  1819.       do_mouse_tracking = 0;
  1820.  
  1821.       /* Redisplay may have been preempted because there was input
  1822.      available, and it assumes it will be called again after the
  1823.      input has been processed.  If the only input available was
  1824.      the sort that we have just disabled, then we need to call
  1825.      redisplay.  */
  1826.       if (!readable_events ())
  1827.     {
  1828.       prepare_menu_bars ();
  1829.       redisplay_preserve_echo_area ();
  1830.       get_input_pending (&input_pending);
  1831.     }
  1832.     }
  1833. }
  1834.  
  1835. DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
  1836.   "Evaluate BODY with mouse movement events enabled.\n\
  1837. Within a `track-mouse' form, mouse motion generates input events that\n\
  1838. you can read with `read-event'.\n\
  1839. Normally, mouse motion is ignored.")
  1840.   (args)
  1841.      Lisp_Object args;
  1842. {
  1843.   int count = specpdl_ptr - specpdl;
  1844.   Lisp_Object val;
  1845.  
  1846.   XSET (val, Lisp_Int, do_mouse_tracking);
  1847.   record_unwind_protect (tracking_off, val);
  1848.  
  1849.   if (!input_pending && !detect_input_pending ())
  1850.     prepare_menu_bars ();
  1851.  
  1852.   do_mouse_tracking = 1;
  1853.   
  1854.   val = Fprogn (args);
  1855.   return unbind_to (count, val);
  1856. }
  1857.  
  1858. /* Low level keyboard/mouse input.
  1859.    kbd_buffer_store_event places events in kbd_buffer, and
  1860.    kbd_buffer_get_event retrieves them.
  1861.    mouse_moved indicates when the mouse has moved again, and
  1862.    *mouse_position_hook provides the mouse position.  */
  1863.  
  1864. /* Return true iff there are any events in the queue that read-char
  1865.    would return.  If this returns false, a read-char would block.  */
  1866. static int
  1867. readable_events ()
  1868. {
  1869.   return ! EVENT_QUEUES_EMPTY;
  1870. }
  1871.  
  1872. /* Set this for debugging, to have a way to get out */
  1873. int stop_character;
  1874.  
  1875. /* Store an event obtained at interrupt level into kbd_buffer, fifo */
  1876.  
  1877. void
  1878. kbd_buffer_store_event (event)
  1879.      register struct input_event *event;
  1880. {
  1881.   if (event->kind == no_event)
  1882.     abort ();
  1883.  
  1884.   if (event->kind == ascii_keystroke)
  1885.     {
  1886.       register int c = event->code & 0377;
  1887.  
  1888.       if (event->modifiers & ctrl_modifier)
  1889.     c = make_ctrl_char (c);
  1890.  
  1891.       c |= (event->modifiers
  1892.         & (meta_modifier | alt_modifier
  1893.            | hyper_modifier | super_modifier));
  1894.  
  1895.       if (c == quit_char)
  1896.     {
  1897.       extern SIGTYPE interrupt_signal ();
  1898.  
  1899. #ifdef MULTI_FRAME
  1900.       /* If this results in a quit_char being returned to Emacs as
  1901.          input, set Vlast_event_frame properly.  If this doesn't
  1902.          get returned to Emacs as an event, the next event read
  1903.          will set Vlast_event_frame again, so this is safe to do.  */
  1904.       {
  1905.         Lisp_Object focus;
  1906.  
  1907.         focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
  1908.         if (NILP (focus))
  1909.           internal_last_event_frame = event->frame_or_window;
  1910.         else
  1911.           internal_last_event_frame = focus;
  1912.         Vlast_event_frame = internal_last_event_frame;
  1913.       }
  1914. #endif
  1915.  
  1916.       last_event_timestamp = event->timestamp;
  1917.       interrupt_signal ();
  1918.       return;
  1919.     }
  1920.  
  1921.       if (c && c == stop_character)
  1922.     {
  1923.       sys_suspend ();
  1924.       return;
  1925.     }
  1926.     }
  1927.  
  1928.   if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
  1929.     kbd_store_ptr = kbd_buffer;
  1930.  
  1931.   /* Don't let the very last slot in the buffer become full,
  1932.      since that would make the two pointers equal,
  1933.      and that is indistinguishable from an empty buffer.
  1934.      Discard the event if it would fill the last slot.  */
  1935.   if (kbd_fetch_ptr - 1 != kbd_store_ptr)
  1936.     {
  1937.       kbd_store_ptr->kind = event->kind;
  1938.       if (event->kind == selection_request_event)
  1939.     {
  1940.       /* We must not use the ordinary copying code for this case,
  1941.          since `part' is an enum and copying it might not copy enough
  1942.          in this case.  */
  1943.       bcopy (event, kbd_store_ptr, sizeof (*event));
  1944.     }
  1945.       else
  1946.     {
  1947.       kbd_store_ptr->code = event->code;
  1948.       kbd_store_ptr->part = event->part;
  1949.       kbd_store_ptr->frame_or_window = event->frame_or_window;
  1950.       kbd_store_ptr->modifiers = event->modifiers;
  1951.       kbd_store_ptr->x = event->x;
  1952.       kbd_store_ptr->y = event->y;
  1953.       kbd_store_ptr->timestamp = event->timestamp;
  1954.     }
  1955.       (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
  1956.                               - kbd_buffer]
  1957.        = event->frame_or_window);
  1958.  
  1959.       kbd_store_ptr++;
  1960.     }
  1961. }
  1962.  
  1963. /* Read one event from the event buffer, waiting if necessary.
  1964.    The value is a Lisp object representing the event.
  1965.    The value is nil for an event that should be ignored,
  1966.    or that was handled here.
  1967.    We always read and discard one event.  */
  1968.  
  1969. static Lisp_Object
  1970. kbd_buffer_get_event ()
  1971. {
  1972.   register int c;
  1973.   Lisp_Object obj;
  1974.  
  1975.   if (noninteractive)
  1976.     {
  1977.       c = getchar ();
  1978.       XSET (obj, Lisp_Int, c);
  1979.       return obj;
  1980.     }
  1981.  
  1982.   /* Wait until there is input available.  */
  1983.   for (;;)
  1984.     {
  1985.       if (!EVENT_QUEUES_EMPTY)
  1986.     break;
  1987.  
  1988.       /* If the quit flag is set, then read_char will return
  1989.      quit_char, so that counts as "available input."  */
  1990.       if (!NILP (Vquit_flag))
  1991.     quit_throw_to_read_char ();
  1992.  
  1993.       /* One way or another, wait until input is available; then, if
  1994.      interrupt handlers have not read it, read it now.  */
  1995.  
  1996. #ifdef OLDVMS
  1997.       wait_for_kbd_input ();
  1998. #else
  1999. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  2000. #ifdef SIGIO
  2001.       gobble_input (0);
  2002. #endif /* SIGIO */
  2003.       if (EVENT_QUEUES_EMPTY)
  2004.     {
  2005.       Lisp_Object minus_one;
  2006.  
  2007.       XSET (minus_one, Lisp_Int, -1);
  2008.       wait_reading_process_input (0, 0, minus_one, 1);
  2009.  
  2010.       if (!interrupt_input && EVENT_QUEUES_EMPTY)
  2011.         /* Pass 1 for EXPECT since we just waited to have input.  */
  2012.         read_avail_input (1);
  2013.     }
  2014. #endif /* not VMS */
  2015.     }
  2016.  
  2017.   /* At this point, we know that there is a readable event available
  2018.      somewhere.  If the event queue is empty, then there must be a
  2019.      mouse movement enabled and available.  */
  2020.   if (kbd_fetch_ptr != kbd_store_ptr)
  2021.     {
  2022.       struct input_event *event;
  2023.  
  2024.       event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
  2025.            ? kbd_fetch_ptr
  2026.            : kbd_buffer);
  2027.  
  2028.       last_event_timestamp = event->timestamp;
  2029.  
  2030.       obj = Qnil;
  2031.  
  2032.       /* These two kinds of events get special handling
  2033.      and don't actually appear to the command loop.
  2034.      We return nil for them.  */
  2035.       if (event->kind == selection_request_event)
  2036.     {
  2037. #ifdef HAVE_X11
  2038.       x_handle_selection_request (event);
  2039.       kbd_fetch_ptr = event + 1;
  2040. #else
  2041.       /* We're getting selection request events, but we don't have
  2042.              a window system.  */
  2043.       abort ();
  2044. #endif
  2045.     }
  2046.  
  2047.       else if (event->kind == selection_clear_event)
  2048.     {
  2049. #ifdef HAVE_X11
  2050.       x_handle_selection_clear (event);
  2051.       kbd_fetch_ptr = event + 1;
  2052. #else
  2053.       /* We're getting selection request events, but we don't have
  2054.              a window system.  */
  2055.       abort ();
  2056. #endif
  2057.     }
  2058. #ifdef HAVE_X11
  2059.       else if (event->kind == delete_window_event)
  2060.     {
  2061.       Lisp_Object tail, frame;
  2062.       struct frame *f;
  2063.     
  2064.       /* If the user destroys the only frame, Emacs should exit.
  2065.          Count visible frames and iconified frames.  */
  2066.       for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
  2067.         {
  2068.           frame = XCONS (tail)->car;
  2069.           if (XTYPE (frame) != Lisp_Frame || EQ (frame, event->frame_or_window))
  2070.         continue;
  2071.           f = XFRAME (frame);
  2072.           if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
  2073.         break;
  2074.         }
  2075.  
  2076.       if (! CONSP (tail))
  2077.         Fkill_emacs (Qnil);
  2078.  
  2079.       Fdelete_frame (event->frame_or_window, Qt);
  2080.       kbd_fetch_ptr = event + 1;
  2081.     }
  2082. #endif
  2083.       else if (event->kind == menu_bar_event)
  2084.     {
  2085.       /* The event value is in the frame_or_window slot.  */
  2086.       obj = event->frame_or_window;
  2087.       kbd_fetch_ptr = event + 1;
  2088.     }
  2089.       else if (event->kind == buffer_switch_event)
  2090.     {
  2091.       /* The value doesn't matter here; only the type is tested.  */
  2092.       XSET (obj, Lisp_Buffer, current_buffer);
  2093.       kbd_fetch_ptr = event + 1;
  2094.     }
  2095.       /* Just discard these, by returning nil.
  2096.      (They shouldn't be found in the buffer,
  2097.      but on some machines it appears they do show up.)  */
  2098.       else if (event->kind == no_event)
  2099.     kbd_fetch_ptr = event + 1;
  2100.  
  2101.       /* If this event is on a different frame, return a switch-frame this
  2102.      time, and leave the event in the queue for next time.  */
  2103.       else
  2104.     {
  2105. #ifdef MULTI_FRAME
  2106.       Lisp_Object frame;
  2107.       Lisp_Object focus;
  2108.  
  2109.       frame = event->frame_or_window;
  2110.       if (XTYPE (frame) == Lisp_Window)
  2111.         frame = WINDOW_FRAME (XWINDOW (frame));
  2112.  
  2113.       focus = FRAME_FOCUS_FRAME (XFRAME (frame));
  2114.       if (! NILP (focus))
  2115.         frame = focus;
  2116.  
  2117.       if (! EQ (frame, internal_last_event_frame)
  2118.           && XFRAME (frame) != selected_frame)
  2119.         obj = make_lispy_switch_frame (frame);
  2120.       internal_last_event_frame = frame;
  2121. #endif /* MULTI_FRAME */
  2122.  
  2123.       /* If we didn't decide to make a switch-frame event, go ahead
  2124.          and build a real event from the queue entry.  */
  2125.  
  2126.       if (NILP (obj))
  2127.         {
  2128.           obj = make_lispy_event (event);
  2129.  
  2130.           /* Wipe out this event, to catch bugs.  */
  2131.           event->kind = no_event;
  2132.           (XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer]
  2133.            = Qnil);
  2134.  
  2135.           kbd_fetch_ptr = event + 1;
  2136.         }
  2137.     }
  2138.     }
  2139.   /* Try generating a mouse motion event.  */
  2140.   else if (do_mouse_tracking && mouse_moved)
  2141.     {
  2142.       FRAME_PTR f = 0;
  2143.       Lisp_Object bar_window;
  2144.       enum scroll_bar_part part;
  2145.       Lisp_Object x, y;
  2146.       unsigned long time;
  2147.  
  2148.       (*mouse_position_hook) (&f, &bar_window, &part, &x, &y, &time);
  2149.  
  2150.       obj = Qnil;
  2151.  
  2152. #ifdef MULTI_FRAME
  2153.       /* Decide if we should generate a switch-frame event.  Don't
  2154.      generate switch-frame events for motion outside of all Emacs
  2155.      frames.  */
  2156.       if (f)
  2157.     {
  2158.       Lisp_Object frame;
  2159.  
  2160.       frame = FRAME_FOCUS_FRAME (f);
  2161.       if (NILP (frame))
  2162.         XSET (frame, Lisp_Frame, f);
  2163.  
  2164.       if (! EQ (frame, internal_last_event_frame)
  2165.           && XFRAME (frame) != selected_frame)
  2166.         obj = make_lispy_switch_frame (frame);
  2167.       internal_last_event_frame = frame;
  2168.     }
  2169.  
  2170.       /* If we didn't decide to make a switch-frame event, go ahead and 
  2171.      return a mouse-motion event.  */
  2172.       if (NILP (obj))
  2173.     obj = make_lispy_movement (f, bar_window, part, x, y, time);
  2174. #endif
  2175.     }
  2176.   else
  2177.     /* We were promised by the above while loop that there was
  2178.        something for us to read!  */
  2179.     abort ();
  2180.  
  2181.   input_pending = readable_events ();
  2182.  
  2183. #ifdef MULTI_FRAME
  2184.   Vlast_event_frame = internal_last_event_frame;
  2185. #endif
  2186.  
  2187.   return (obj);
  2188. }
  2189.  
  2190. /* Process any events that are not user-visible,
  2191.    then return, without reading any user-visible events.  */
  2192.  
  2193. void
  2194. swallow_events ()
  2195. {
  2196.   while (kbd_fetch_ptr != kbd_store_ptr)
  2197.     {
  2198.       struct input_event *event;
  2199.  
  2200.       event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
  2201.            ? kbd_fetch_ptr
  2202.            : kbd_buffer);
  2203.  
  2204.       last_event_timestamp = event->timestamp;
  2205.  
  2206.       /* These two kinds of events get special handling
  2207.      and don't actually appear to the command loop.  */
  2208.       if (event->kind == selection_request_event)
  2209.     {
  2210. #ifdef HAVE_X11
  2211.       x_handle_selection_request (event);
  2212.       kbd_fetch_ptr = event + 1;
  2213. #else
  2214.       /* We're getting selection request events, but we don't have
  2215.              a window system.  */
  2216.       abort ();
  2217. #endif
  2218.     }
  2219.  
  2220.       else if (event->kind == selection_clear_event)
  2221.     {
  2222. #ifdef HAVE_X11
  2223.       x_handle_selection_clear (event);
  2224.       kbd_fetch_ptr = event + 1;
  2225. #else
  2226.       /* We're getting selection request events, but we don't have
  2227.              a window system.  */
  2228.       abort ();
  2229. #endif
  2230.     }
  2231.       else
  2232.     break;
  2233.     }
  2234.  
  2235.   get_input_pending (&input_pending);
  2236. }
  2237.  
  2238. /* Caches for modify_event_symbol.  */
  2239. static Lisp_Object accent_key_syms;
  2240. static Lisp_Object system_key_syms;
  2241. static Lisp_Object func_key_syms;
  2242. static Lisp_Object mouse_syms;
  2243.  
  2244. Lisp_Object Vsystem_key_alist;
  2245.  
  2246. /* This is a list of keysym codes for special "accent" characters.
  2247.    It parallels lispy_accent_keys.  */
  2248.  
  2249. static int FAR lispy_accent_codes[] =
  2250. {
  2251. #ifdef XK_dead_circumflex
  2252.   XK_dead_circumflex,
  2253. #else
  2254.   0,
  2255. #endif
  2256. #ifdef XK_dead_grave
  2257.   XK_dead_grave,
  2258. #else
  2259.   0,
  2260. #endif
  2261. #ifdef XK_dead_tilde
  2262.   XK_dead_tilde,
  2263. #else
  2264.   0,
  2265. #endif
  2266. #ifdef XK_dead_diaeresis
  2267.   XK_dead_diaeresis,
  2268. #else
  2269.   0,
  2270. #endif
  2271. #ifdef XK_dead_macron
  2272.   XK_dead_macron,
  2273. #else
  2274.   0,
  2275. #endif
  2276. #ifdef XK_dead_degree
  2277.   XK_dead_degree,
  2278. #else
  2279.   0,
  2280. #endif
  2281. #ifdef XK_dead_acute
  2282.   XK_dead_acute,
  2283. #else
  2284.   0,
  2285. #endif
  2286. #ifdef XK_dead_cedilla
  2287.   XK_dead_cedilla,
  2288. #else
  2289.   0,
  2290. #endif
  2291. #ifdef XK_dead_breve
  2292.   XK_dead_breve,
  2293. #else
  2294.   0,
  2295. #endif
  2296. #ifdef XK_dead_ogonek
  2297.   XK_dead_ogonek,
  2298. #else
  2299.   0,
  2300. #endif
  2301. #ifdef XK_dead_caron
  2302.   XK_dead_caron,
  2303. #else
  2304.   0,
  2305. #endif
  2306. #ifdef XK_dead_doubleacute
  2307.   XK_dead_doubleacute,
  2308. #else
  2309.   0,
  2310. #endif
  2311. #ifdef XK_dead_abovedot
  2312.   XK_dead_abovedot,
  2313. #else
  2314.   0,
  2315. #endif
  2316. };
  2317.  
  2318. /* This is a list of Lisp names for special "accent" characters.
  2319.    It parallels lispy_accent_codes.  */
  2320.  
  2321. static char * FAR lispy_accent_keys[] =
  2322. {
  2323.   "dead-circumflex",
  2324.   "dead-grave",
  2325.   "dead-tilde",
  2326.   "dead-diaeresis",
  2327.   "dead-macron",
  2328.   "dead-degree",
  2329.   "dead-acute",
  2330.   "dead-cedilla",
  2331.   "dead-breve",
  2332.   "dead-ogonek",
  2333.   "dead-caron",
  2334.   "dead-doubleacute",
  2335.   "dead-abovedot",
  2336. };
  2337.  
  2338. /* You'll notice that this table is arranged to be conveniently
  2339.    indexed by X Windows keysym values.  */
  2340. static char * FAR lispy_function_keys[] =
  2341.   {
  2342.     /* X Keysym value */
  2343.  
  2344.     0, 0, 0, 0, 0, 0, 0, 0,    /* 0xff00 */
  2345.     "backspace",
  2346.     "tab",
  2347.     "linefeed",
  2348.     "clear",
  2349.     0,
  2350.     "return",
  2351.     0, 0,
  2352.     0, 0, 0,            /* 0xff10 */
  2353.     "pause",
  2354.     0, 0, 0, 0, 0, 0, 0,
  2355.     "escape",
  2356.     0, 0, 0, 0,
  2357.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0xff20...2f */
  2358.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0xff30...3f */
  2359.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0xff40...4f */
  2360.  
  2361.     "home",            /* 0xff50 */    /* IsCursorKey */
  2362.     "left",
  2363.     "up",
  2364.     "right",
  2365.     "down",
  2366.     "prior",
  2367.     "next",
  2368.     "end",
  2369.     "begin",
  2370.     0,                /* 0xff59 */
  2371.     0, 0, 0, 0, 0, 0,
  2372.     "select",            /* 0xff60 */    /* IsMiscFunctionKey */
  2373.     "print",
  2374.     "execute",
  2375.     "insert",
  2376.     0,        /* 0xff64 */
  2377.     "undo",
  2378.     "redo",
  2379.     "menu",
  2380.     "find",
  2381.     "cancel",
  2382.     "help",
  2383.     "break",            /* 0xff6b */
  2384.  
  2385.     0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
  2386.     0,                /* 0xff76 */
  2387.     0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock",    /* 0xff7f */
  2388.     "kp-space",            /* 0xff80 */    /* IsKeypadKey */
  2389.     0, 0, 0, 0, 0, 0, 0, 0,
  2390.     "kp-tab",            /* 0xff89 */
  2391.     0, 0, 0,
  2392.     "kp-enter",            /* 0xff8d */
  2393.     0, 0, 0,
  2394.     "kp-f1",            /* 0xff91 */
  2395.     "kp-f2",
  2396.     "kp-f3",
  2397.     "kp-f4",
  2398.     "kp-home",            /* 0xff95 */
  2399.     "kp-left",
  2400.     "kp-up",
  2401.     "kp-right",
  2402.     "kp-down",
  2403.     "kp-prior",            /* kp-page-up */
  2404.     "kp-next",            /* kp-page-down */
  2405.     "kp-end",
  2406.     "kp-begin",
  2407.     "kp-insert",
  2408.     "kp-delete",
  2409.     0,                /* 0xffa0 */
  2410.     0, 0, 0, 0, 0, 0, 0, 0, 0,
  2411.     "kp-multiply",        /* 0xffaa */
  2412.     "kp-add",
  2413.     "kp-separator",
  2414.     "kp-subtract",
  2415.     "kp-decimal",
  2416.     "kp-divide",        /* 0xffaf */
  2417.     "kp-0",            /* 0xffb0 */
  2418.     "kp-1",    "kp-2",    "kp-3",    "kp-4",    "kp-5",    "kp-6",    "kp-7",    "kp-8",    "kp-9",
  2419.     0,        /* 0xffba */
  2420.     0, 0,
  2421.     "kp-equal",            /* 0xffbd */
  2422.     "f1",            /* 0xffbe */    /* IsFunctionKey */
  2423.     "f2",
  2424.     "f3", "f4", "f5", "f6", "f7", "f8",    "f9", "f10", /* 0xffc0 */
  2425.     "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
  2426.     "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
  2427.     "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
  2428.     "f35", 0, 0, 0, 0, 0, 0, 0,    /* 0xffe0 */
  2429.     0, 0, 0, 0, 0, 0, 0, 0,
  2430.     0, 0, 0, 0, 0, 0, 0, 0,     /* 0xfff0 */
  2431.     0, 0, 0, 0, 0, 0, 0, "delete"
  2432.     };
  2433.  
  2434. static char * FAR lispy_mouse_names[] = 
  2435. {
  2436.   "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
  2437. };
  2438.  
  2439. /* Scroll bar parts.  */
  2440. Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
  2441.  
  2442. /* An array of scroll bar parts, indexed by an enum scroll_bar_part value.  */
  2443. Lisp_Object *scroll_bar_parts[] = {
  2444.   &Qabove_handle, &Qhandle, &Qbelow_handle
  2445. };
  2446.  
  2447.  
  2448. /* A vector, indexed by button number, giving the down-going location
  2449.    of currently depressed buttons, both scroll bar and non-scroll bar.
  2450.  
  2451.    The elements have the form
  2452.      (BUTTON-NUMBER MODIFIER-MASK . REST)
  2453.    where REST is the cdr of a position as it would be reported in the event.
  2454.  
  2455.    The make_lispy_event function stores positions here to tell the
  2456.    difference between click and drag events, and to store the starting
  2457.    location to be included in drag events.  */
  2458.  
  2459. static Lisp_Object button_down_location;
  2460.  
  2461. /* Information about the most recent up-going button event:  Which
  2462.    button, what location, and what time. */
  2463.  
  2464. static int last_mouse_button;
  2465. static int last_mouse_x;
  2466. static int last_mouse_y;
  2467. static unsigned long button_down_time;
  2468.  
  2469. /* The maximum time between clicks to make a double-click,
  2470.    or Qnil to disable double-click detection,
  2471.    or Qt for no time limit.  */
  2472. Lisp_Object Vdouble_click_time;
  2473.  
  2474. /* The number of clicks in this multiple-click. */
  2475.  
  2476. int double_click_count;
  2477.  
  2478. #ifdef USE_EXTERNAL_MENU_BAR
  2479. extern Lisp_Object map_event_to_object ();
  2480. #endif /* USE_EXTERNAL_MENU_BAR  */
  2481.  
  2482. /* Given a struct input_event, build the lisp event which represents
  2483.    it.  If EVENT is 0, build a mouse movement event from the mouse
  2484.    movement buffer, which should have a movement event in it.
  2485.  
  2486.    Note that events must be passed to this function in the order they
  2487.    are received; this function stores the location of button presses
  2488.    in order to build drag events when the button is released.  */
  2489.  
  2490. static Lisp_Object
  2491. make_lispy_event (event)
  2492.      struct input_event *event;
  2493. {
  2494.   int i;
  2495.  
  2496. #ifdef SWITCH_ENUM_BUG
  2497.   switch ((int) event->kind)
  2498. #else
  2499.   switch (event->kind)
  2500. #endif
  2501.     {
  2502.       /* A simple keystroke.  */
  2503.     case ascii_keystroke:
  2504.       {
  2505.     int c = event->code & 0377;
  2506.     /* Turn ASCII characters into control characters
  2507.        when proper.  */
  2508.     if (event->modifiers & ctrl_modifier)
  2509.       c = make_ctrl_char (c);
  2510.  
  2511.     /* Add in the other modifier bits.  We took care of ctrl_modifier
  2512.        just above, and the shift key was taken care of by the X code,
  2513.        and applied to control characters by make_ctrl_char.  */
  2514.     c |= (event->modifiers
  2515.           & (meta_modifier | alt_modifier
  2516.          | hyper_modifier | super_modifier));
  2517.     button_down_time = 0;
  2518.     return c;
  2519.       }
  2520.  
  2521.       /* A function key.  The symbol may need to have modifier prefixes
  2522.      tacked onto it.  */
  2523.     case non_ascii_keystroke:
  2524.       button_down_time = 0;
  2525.  
  2526.       for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
  2527.     if (event->code == lispy_accent_codes[i])
  2528.       return modify_event_symbol (i,
  2529.                       event->modifiers,
  2530.                       Qfunction_key, Qnil,
  2531.                       lispy_accent_keys, &accent_key_syms,
  2532.                       (sizeof (lispy_accent_keys)
  2533.                        / sizeof (lispy_accent_keys[0])));
  2534.  
  2535.       /* Handle system-specific keysyms.  */
  2536.       if (event->code & (1 << 28))
  2537.     {
  2538.       /* We need to use an alist rather than a vector as the cache
  2539.          since we can't make a vector long enuf.  */
  2540.       if (NILP (system_key_syms))
  2541.         system_key_syms = Fcons (Qnil, Qnil);
  2542.       return modify_event_symbol (event->code & 0xffffff,
  2543.                       event->modifiers,
  2544.                       Qfunction_key, Vsystem_key_alist,
  2545.                       0, &system_key_syms, 0xffffff);
  2546.     }
  2547.  
  2548.       return modify_event_symbol (event->code - 0xff00,
  2549.                   event->modifiers,
  2550.                   Qfunction_key, Qnil,
  2551.                   lispy_function_keys, &func_key_syms,
  2552.                   (sizeof (lispy_function_keys)
  2553.                    / sizeof (lispy_function_keys[0])));
  2554.       break;
  2555.  
  2556. #if defined(MULTI_FRAME) || defined(HAVE_MOUSE)
  2557.       /* A mouse click.  Figure out where it is, decide whether it's 
  2558.          a press, click or drag, and build the appropriate structure.  */
  2559.     case mouse_click:
  2560.     case scroll_bar_click:
  2561.       {
  2562.     int button = event->code;
  2563.     int is_double;
  2564.     Lisp_Object position;
  2565.     Lisp_Object *start_pos_ptr;
  2566.     Lisp_Object start_pos;
  2567.  
  2568.     if (button < 0 || button >= NUM_MOUSE_BUTTONS)
  2569.       abort ();
  2570.  
  2571.     /* Build the position as appropriate for this mouse click.  */
  2572.     if (event->kind == mouse_click)
  2573.       {
  2574.         int part;
  2575.         FRAME_PTR f = XFRAME (event->frame_or_window);
  2576.         Lisp_Object window;
  2577.         Lisp_Object posn;
  2578.         int row, column;
  2579.  
  2580.         /* Ignore mouse events that were made on frame that
  2581.            have been deleted.  */
  2582.         if (! FRAME_LIVE_P (f))
  2583.           return Qnil;
  2584.  
  2585.         pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
  2586.                    &column, &row, 0, 0);
  2587.  
  2588. #ifdef USE_EXTERNAL_MENU_BAR
  2589.         if (FRAME_EXTERNAL_MENU_BAR (f) && XINT (event->y) == -1)
  2590. #else
  2591.         if (row < FRAME_MENU_BAR_LINES (f))
  2592. #endif
  2593.           {
  2594.         Lisp_Object items, item;
  2595.  
  2596. #ifdef USE_EXTERNAL_MENU_BAR
  2597.         /* The click happened in the menubar.
  2598.            Look for the menu item selected.  */
  2599.         item = map_event_to_object (event, f);
  2600.  
  2601.         XFASTINT (event->y) = 1;
  2602. #else /* not USE_EXTERNAL_MENU_BAR  */
  2603.         int hpos;
  2604.         int i;
  2605.  
  2606.         item = Qnil;
  2607.         items = FRAME_MENU_BAR_ITEMS (f);
  2608.         for (i = 0; i < XVECTOR (items)->size; i += 3)
  2609.           {
  2610.             Lisp_Object pos, string;
  2611.             string = XVECTOR (items)->contents[i + 1];
  2612.             pos = XVECTOR (items)->contents[i + 2];
  2613.             if (NILP (string))
  2614.               break;
  2615.             if (column >= XINT (pos)
  2616.             && column < XINT (pos) + XSTRING (string)->size)
  2617.               {
  2618.             item = XVECTOR (items)->contents[i];
  2619.             break;
  2620.               }
  2621.           }
  2622. #endif /* not USE_EXTERNAL_MENU_BAR  */
  2623.  
  2624.         position
  2625.           = Fcons (event->frame_or_window,
  2626.                Fcons (Qmenu_bar,
  2627.                   Fcons (Fcons (event->x, event->y),
  2628.                      Fcons (make_number (event->timestamp),
  2629.                         Qnil))));
  2630.  
  2631.         return Fcons (item, Fcons (position, Qnil));
  2632.           }
  2633.  
  2634.         window = window_from_coordinates (f, column, row, &part);
  2635.  
  2636.         if (XTYPE (window) != Lisp_Window)
  2637.           posn = Qnil;
  2638.         else
  2639.           {
  2640.         int pixcolumn, pixrow;
  2641.         column -= XINT (XWINDOW (window)->left);
  2642.         row -= XINT (XWINDOW (window)->top);
  2643.         glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
  2644.         XSETINT (event->x, pixcolumn);
  2645.         XSETINT (event->y, pixrow);
  2646.  
  2647.         if (part == 1)
  2648.           posn = Qmode_line;
  2649.         else if (part == 2)
  2650.           posn = Qvertical_line;
  2651.         else
  2652.           XSET (posn, Lisp_Int,
  2653.             buffer_posn_from_coords (XWINDOW (window),
  2654.                          column, row));
  2655.           }
  2656.  
  2657.         position
  2658.           = Fcons (window,
  2659.                Fcons (posn,
  2660.                   Fcons (Fcons (event->x, event->y),
  2661.                      Fcons (make_number (event->timestamp),
  2662.                         Qnil))));
  2663.       }
  2664.     else
  2665.       {
  2666.         Lisp_Object window;
  2667.         Lisp_Object portion_whole;
  2668.         Lisp_Object part;
  2669.  
  2670.         window = event->frame_or_window;
  2671.         portion_whole = Fcons (event->x, event->y);
  2672.         part = *scroll_bar_parts[(int) event->part];
  2673.  
  2674.         position =
  2675.           Fcons (window,
  2676.              Fcons (Qvertical_scroll_bar,
  2677.                 Fcons (portion_whole,
  2678.                    Fcons (make_number (event->timestamp),
  2679.                       Fcons (part, Qnil)))));
  2680.       }
  2681.  
  2682.     start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
  2683.  
  2684.     start_pos = *start_pos_ptr;
  2685.     *start_pos_ptr = Qnil;
  2686.  
  2687.     is_double = (button == last_mouse_button
  2688.              && XINT (event->x) == last_mouse_x
  2689.              && XINT (event->y) == last_mouse_y
  2690.              && button_down_time != 0
  2691.              && (EQ (Vdouble_click_time, Qt)
  2692.              || (INTEGERP (Vdouble_click_time)
  2693.                  && ((int)(event->timestamp - button_down_time)
  2694.                  < XINT (Vdouble_click_time)))));
  2695.     last_mouse_button = button;
  2696.     last_mouse_x = XINT (event->x);
  2697.     last_mouse_y = XINT (event->y);
  2698.  
  2699.     /* If this is a button press, squirrel away the location, so
  2700.            we can decide later whether it was a click or a drag.  */
  2701.     if (event->modifiers & down_modifier)
  2702.       {
  2703.         if (is_double)
  2704.           {
  2705.         double_click_count++;
  2706.         event->modifiers |= ((double_click_count > 2)
  2707.                      ? triple_modifier
  2708.                      : double_modifier);
  2709.           }
  2710.         else
  2711.           double_click_count = 1;
  2712.         button_down_time = event->timestamp;
  2713.         *start_pos_ptr = Fcopy_alist (position);
  2714.       }
  2715.  
  2716.     /* Now we're releasing a button - check the co-ordinates to
  2717.            see if this was a click or a drag.  */
  2718.     else if (event->modifiers & up_modifier)
  2719.       {
  2720.         /* If we did not see a down before this up,
  2721.            ignore the up.  Probably this happened because
  2722.            the down event chose a menu item.
  2723.            It would be an annoyance to treat the release
  2724.            of the button that chose the menu item
  2725.            as a separate event.  */
  2726.  
  2727.         if (XTYPE (start_pos) != Lisp_Cons)
  2728.           return Qnil;
  2729.  
  2730.         event->modifiers &= ~up_modifier;
  2731. #if 0 /* Formerly we treated an up with no down as a click event.  */
  2732.         if (XTYPE (start_pos) != Lisp_Cons)
  2733.           event->modifiers |= click_modifier;
  2734.         else
  2735. #endif
  2736.           {
  2737.         /* The third element of every position should be the (x,y)
  2738.            pair.  */
  2739.         Lisp_Object down;
  2740.  
  2741.         down = Fnth (make_number (2), start_pos);
  2742.         if (EQ (event->x, XCONS (down)->car)
  2743.             && EQ (event->y, XCONS (down)->cdr))
  2744.           {
  2745.             if (is_double && double_click_count > 1)
  2746.               event->modifiers |= ((double_click_count > 2)
  2747.                        ? triple_modifier
  2748.                        : double_modifier);
  2749.             else
  2750.               event->modifiers |= click_modifier;
  2751.           }
  2752.         else
  2753.           {
  2754.             button_down_time = 0;
  2755.             event->modifiers |= drag_modifier;
  2756.           }
  2757.           }
  2758.       }
  2759.     else
  2760.       /* Every mouse event should either have the down_modifier or
  2761.              the up_modifier set.  */
  2762.       abort ();
  2763.  
  2764.     {
  2765.       /* Get the symbol we should use for the mouse click.  */
  2766.       Lisp_Object head;
  2767.  
  2768.       head = modify_event_symbol (button,
  2769.                       event->modifiers,
  2770.                       Qmouse_click, Qnil,
  2771.                       lispy_mouse_names, &mouse_syms,
  2772.                       (sizeof (lispy_mouse_names)
  2773.                        / sizeof (lispy_mouse_names[0])));
  2774.       if (event->modifiers & drag_modifier)
  2775.         return Fcons (head,
  2776.               Fcons (start_pos,
  2777.                  Fcons (position,
  2778.                     Qnil)));
  2779.       else if (event->modifiers & (double_modifier | triple_modifier))
  2780.         return Fcons (head,
  2781.               Fcons (position,
  2782.                  Fcons (make_number (double_click_count),
  2783.                     Qnil)));
  2784.       else
  2785.         return Fcons (head,
  2786.               Fcons (position,
  2787.                  Qnil));
  2788.     }
  2789.       }
  2790. #endif /* MULTI_FRAME or HAVE_MOUSE */
  2791.  
  2792.       /* The 'kind' field of the event is something we don't recognize.  */
  2793.     default:
  2794.       abort ();
  2795.     }
  2796. }
  2797.  
  2798. #ifdef MULTI_FRAME
  2799.  
  2800. static Lisp_Object
  2801. make_lispy_movement (frame, bar_window, part, x, y, time)
  2802.      FRAME_PTR frame;
  2803.      Lisp_Object bar_window;
  2804.      enum scroll_bar_part part;
  2805.      Lisp_Object x, y;
  2806.      unsigned long time;
  2807. {
  2808.   /* Is it a scroll bar movement?  */
  2809.   if (frame && ! NILP (bar_window))
  2810.     {
  2811.       Lisp_Object part_sym;
  2812.  
  2813.       part_sym = *scroll_bar_parts[(int) part];
  2814.       return Fcons (Qscroll_bar_movement,
  2815.             (Fcons (Fcons (bar_window,
  2816.                    Fcons (Qvertical_scroll_bar,
  2817.                       Fcons (Fcons (x, y),
  2818.                          Fcons (make_number (time),
  2819.                             Fcons (part_sym,
  2820.                                    Qnil))))),
  2821.                 Qnil)));
  2822.     }
  2823.  
  2824.   /* Or is it an ordinary mouse movement?  */
  2825.   else
  2826.     {
  2827.       int area;
  2828.       Lisp_Object window;
  2829.       Lisp_Object posn;
  2830.       int column, row;
  2831.  
  2832.       if (frame)
  2833.     {
  2834.       /* It's in a frame; which window on that frame?  */
  2835.       pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row, 0, 1);
  2836.       window = window_from_coordinates (frame, column, row, &area);
  2837.     }
  2838.       else
  2839.     window = Qnil;
  2840.  
  2841.       if (XTYPE (window) == Lisp_Window)
  2842.     {
  2843.       int pixcolumn, pixrow;
  2844.       column -= XINT (XWINDOW (window)->left);
  2845.       row -= XINT (XWINDOW (window)->top);
  2846.       glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
  2847.       XSETINT (x, pixcolumn);
  2848.       XSETINT (y, pixrow);
  2849.  
  2850.       if (area == 1)
  2851.         posn = Qmode_line;
  2852.       else if (area == 2)
  2853.         posn = Qvertical_line;
  2854.       else
  2855.         XSET (posn, Lisp_Int,
  2856.           buffer_posn_from_coords (XWINDOW (window), column, row));
  2857.     }
  2858.       else if (frame != 0)
  2859.     {
  2860.       XSET (window, Lisp_Frame, frame);
  2861.       posn = Qnil;
  2862.     }
  2863.       else
  2864.     {
  2865.       window = Qnil;
  2866.       posn = Qnil;
  2867.       XFASTINT (x) = 0;
  2868.       XFASTINT (y) = 0;
  2869.     }
  2870.  
  2871.       return Fcons (Qmouse_movement,
  2872.             Fcons (Fcons (window,
  2873.                   Fcons (posn,
  2874.                      Fcons (Fcons (x, y),
  2875.                         Fcons (make_number (time),
  2876.                                Qnil)))),
  2877.                Qnil));
  2878.     }
  2879. }
  2880.  
  2881. #endif /* MULTI_FRAME */
  2882.  
  2883. /* Construct a switch frame event.  */
  2884. static Lisp_Object
  2885. make_lispy_switch_frame (frame)
  2886.      Lisp_Object frame;
  2887. {
  2888.   return Fcons (Qswitch_frame, Fcons (frame, Qnil));
  2889. }
  2890.  
  2891. /* Manipulating modifiers.  */
  2892.  
  2893. /* Parse the name of SYMBOL, and return the set of modifiers it contains.
  2894.  
  2895.    If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
  2896.    SYMBOL's name of the end of the modifiers; the string from this
  2897.    position is the unmodified symbol name.
  2898.  
  2899.    This doesn't use any caches.  */
  2900. static int
  2901. parse_modifiers_uncached (symbol, modifier_end)
  2902.      Lisp_Object symbol;
  2903.      int *modifier_end;
  2904. {
  2905.   struct Lisp_String *name;
  2906.   int i;
  2907.   int modifiers;
  2908.  
  2909.   CHECK_SYMBOL (symbol, 1);
  2910.   
  2911.   modifiers = 0;
  2912.   name = XSYMBOL (symbol)->name;
  2913.  
  2914.  
  2915.   for (i = 0; i+2 <= name->size; )
  2916.     switch (name->data[i])
  2917.       {
  2918. #define SINGLE_LETTER_MOD(bit)                    \
  2919.         if (name->data[i+1] != '-')                \
  2920.       goto no_more_modifiers;                \
  2921.     modifiers |= bit;                    \
  2922.     i += 2;
  2923.  
  2924.       case 'A':
  2925.     SINGLE_LETTER_MOD (alt_modifier);
  2926.     break;
  2927.  
  2928.       case 'C':
  2929.     SINGLE_LETTER_MOD (ctrl_modifier);
  2930.     break;
  2931.  
  2932.       case 'H':
  2933.     SINGLE_LETTER_MOD (hyper_modifier);
  2934.     break;
  2935.  
  2936.       case 'M':
  2937.     SINGLE_LETTER_MOD (meta_modifier);
  2938.     break;
  2939.  
  2940.       case 'S':
  2941.     SINGLE_LETTER_MOD (shift_modifier);
  2942.     break;
  2943.  
  2944.       case 's':
  2945.     SINGLE_LETTER_MOD (super_modifier);
  2946.     break;
  2947.  
  2948.       case 'd':
  2949.     if (i + 5 > name->size)
  2950.       goto no_more_modifiers;
  2951.     if (! strncmp (name->data + i, "drag-", 5))
  2952.       {
  2953.         modifiers |= drag_modifier;
  2954.         i += 5;
  2955.       }
  2956.     else if (! strncmp (name->data + i, "down-", 5))
  2957.       {
  2958.         modifiers |= down_modifier;
  2959.         i += 5;
  2960.       }
  2961.     else if (i + 7 <= name->size
  2962.          && ! strncmp (name->data + i, "double-", 7))
  2963.       {
  2964.         modifiers |= double_modifier;
  2965.         i += 7;
  2966.       }
  2967.     else
  2968.       goto no_more_modifiers;
  2969.     break;
  2970.  
  2971.       case 't':
  2972.     if (i + 7 > name->size)
  2973.       goto no_more_modifiers;
  2974.     if (! strncmp (name->data + i, "triple-", 7))
  2975.       {
  2976.         modifiers |= triple_modifier;
  2977.         i += 7;
  2978.       }
  2979.     else
  2980.       goto no_more_modifiers;
  2981.     break;
  2982.  
  2983.       default:
  2984.     goto no_more_modifiers;
  2985.  
  2986. #undef SINGLE_LETTER_MOD
  2987.       }
  2988.  no_more_modifiers:
  2989.  
  2990.   /* Should we include the `click' modifier?  */
  2991.   if (! (modifiers & (down_modifier | drag_modifier
  2992.               | double_modifier | triple_modifier))
  2993.       && i + 7 == name->size
  2994.       && strncmp (name->data + i, "mouse-", 6) == 0
  2995.       && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
  2996.     modifiers |= click_modifier;
  2997.  
  2998.   if (modifier_end)
  2999.     *modifier_end = i;
  3000.  
  3001.   return modifiers;
  3002. }
  3003.  
  3004.  
  3005. /* Return a symbol whose name is the modifier prefixes for MODIFIERS
  3006.    prepended to the string BASE[0..BASE_LEN-1].
  3007.    This doesn't use any caches.  */
  3008. static Lisp_Object
  3009. apply_modifiers_uncached (modifiers, base, base_len)
  3010.      int modifiers;
  3011.      char *base;
  3012.      int base_len;
  3013. {
  3014.   /* Since BASE could contain nulls, we can't use intern here; we have
  3015.      to use Fintern, which expects a genuine Lisp_String, and keeps a
  3016.      reference to it.  */
  3017.   char *new_mods =
  3018.     (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
  3019.   int mod_len;
  3020.  
  3021.   {
  3022.     char *p = new_mods;
  3023.  
  3024.     /* Only the event queue may use the `up' modifier; it should always
  3025.        be turned into a click or drag event before presented to lisp code.  */
  3026.     if (modifiers & up_modifier)
  3027.       abort ();
  3028.  
  3029.     if (modifiers & alt_modifier)   { *p++ = 'A'; *p++ = '-'; }
  3030.     if (modifiers & ctrl_modifier)  { *p++ = 'C'; *p++ = '-'; }
  3031.     if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
  3032.     if (modifiers & meta_modifier)  { *p++ = 'M'; *p++ = '-'; }
  3033.     if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
  3034.     if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
  3035.     if (modifiers & double_modifier)  { strcpy (p, "double-");  p += 7; }
  3036.     if (modifiers & triple_modifier)  { strcpy (p, "triple-");  p += 7; }
  3037.     if (modifiers & down_modifier)  { strcpy (p, "down-");  p += 5; }
  3038.     if (modifiers & drag_modifier)  { strcpy (p, "drag-");  p += 5; }
  3039.     /* The click modifier is denoted by the absence of other modifiers.  */
  3040.  
  3041.     *p = '\0';
  3042.  
  3043.     mod_len = p - new_mods;
  3044.   }
  3045.  
  3046.   {
  3047.     Lisp_Object new_name;
  3048.     
  3049.     new_name = make_uninit_string (mod_len + base_len);
  3050.     bcopy (new_mods, XSTRING (new_name)->data,           mod_len);
  3051.     bcopy (base,     XSTRING (new_name)->data + mod_len, base_len);
  3052.  
  3053.     return Fintern (new_name, Qnil);
  3054.   }
  3055. }
  3056.  
  3057.  
  3058. static char * FAR modifier_names[] =
  3059. {
  3060.   "up", "down", "drag", "click", "double", "triple", 0, 0,
  3061.   0, 0, 0, 0, 0, 0, 0, 0,
  3062.   0, 0, "alt", "super", "hyper", "shift", "control", "meta"
  3063. };
  3064. #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
  3065.  
  3066. static Lisp_Object modifier_symbols;
  3067.  
  3068. /* Return the list of modifier symbols corresponding to the mask MODIFIERS.  */
  3069. static Lisp_Object
  3070. lispy_modifier_list (modifiers)
  3071.      int modifiers;
  3072. {
  3073.   Lisp_Object modifier_list;
  3074.   int i;
  3075.  
  3076.   modifier_list = Qnil;
  3077.   for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
  3078.     if (modifiers & (1<<i))
  3079.       modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
  3080.                  modifier_list);
  3081.  
  3082.   return modifier_list;
  3083. }
  3084.  
  3085.  
  3086. /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
  3087.    where UNMODIFIED is the unmodified form of SYMBOL,
  3088.    MASK is the set of modifiers present in SYMBOL's name.
  3089.    This is similar to parse_modifiers_uncached, but uses the cache in
  3090.    SYMBOL's Qevent_symbol_element_mask property, and maintains the
  3091.    Qevent_symbol_elements property.  */
  3092. static Lisp_Object
  3093. parse_modifiers (symbol)
  3094.      Lisp_Object symbol;
  3095. {
  3096.   Lisp_Object elements;
  3097.  
  3098.   elements = Fget (symbol, Qevent_symbol_element_mask);
  3099.   if (CONSP (elements))
  3100.     return elements;
  3101.   else
  3102.     {
  3103.       int end;
  3104.       int modifiers = parse_modifiers_uncached (symbol, &end);
  3105.       Lisp_Object unmodified;
  3106.       Lisp_Object mask;
  3107.  
  3108.       unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
  3109.                      XSYMBOL (symbol)->name->size - end),
  3110.                 Qnil);
  3111.  
  3112.       if (modifiers & ~((1<<VALBITS) - 1))
  3113.     abort ();
  3114.       XFASTINT (mask) = modifiers;
  3115.       elements = Fcons (unmodified, Fcons (mask, Qnil));
  3116.  
  3117.       /* Cache the parsing results on SYMBOL.  */
  3118.       Fput (symbol, Qevent_symbol_element_mask,
  3119.         elements);
  3120.       Fput (symbol, Qevent_symbol_elements,
  3121.         Fcons (unmodified, lispy_modifier_list (modifiers)));
  3122.  
  3123.       /* Since we know that SYMBOL is modifiers applied to unmodified,
  3124.      it would be nice to put that in unmodified's cache.
  3125.      But we can't, since we're not sure that parse_modifiers is
  3126.      canonical.  */
  3127.  
  3128.       return elements;
  3129.     }
  3130. }
  3131.  
  3132. /* Apply the modifiers MODIFIERS to the symbol BASE.
  3133.    BASE must be unmodified.
  3134.  
  3135.    This is like apply_modifiers_uncached, but uses BASE's
  3136.    Qmodifier_cache property, if present.  It also builds
  3137.    Qevent_symbol_elements properties, since it has that info anyway.
  3138.  
  3139.    apply_modifiers copies the value of BASE's Qevent_kind property to
  3140.    the modified symbol.  */
  3141. static Lisp_Object
  3142. apply_modifiers (modifiers, base)
  3143.      int modifiers;
  3144.      Lisp_Object base;
  3145. {
  3146.   Lisp_Object cache, index, entry, new_symbol;
  3147.  
  3148.   /* Mask out upper bits.  We don't know where this value's been.  */
  3149.   modifiers &= (1<<VALBITS) - 1;
  3150.  
  3151.   /* The click modifier never figures into cache indices.  */
  3152.   cache = Fget (base, Qmodifier_cache);
  3153.   XFASTINT (index) = (modifiers & ~click_modifier);
  3154.   entry = Fassq (index, cache);
  3155.  
  3156.   if (CONSP (entry))
  3157.     new_symbol = XCONS (entry)->cdr;
  3158.   else
  3159.     {
  3160.       /* We have to create the symbol ourselves.  */  
  3161.       new_symbol = apply_modifiers_uncached (modifiers,
  3162.                          XSYMBOL (base)->name->data,
  3163.                          XSYMBOL (base)->name->size);
  3164.  
  3165.       /* Add the new symbol to the base's cache.  */
  3166.       entry = Fcons (index, new_symbol);
  3167.       Fput (base, Qmodifier_cache, Fcons (entry, cache));
  3168.  
  3169.       /* We have the parsing info now for free, so add it to the caches.  */
  3170.       XFASTINT (index) = modifiers;
  3171.       Fput (new_symbol, Qevent_symbol_element_mask,
  3172.         Fcons (base, Fcons (index, Qnil)));
  3173.       Fput (new_symbol, Qevent_symbol_elements,
  3174.         Fcons (base, lispy_modifier_list (modifiers)));
  3175.     }
  3176.  
  3177.   /* Make sure this symbol is of the same kind as BASE.  
  3178.  
  3179.      You'd think we could just set this once and for all when we
  3180.      intern the symbol above, but reorder_modifiers may call us when
  3181.      BASE's property isn't set right; we can't assume that just
  3182.      because it has a Qmodifier_cache property it must have its
  3183.      Qevent_kind set right as well.  */
  3184.   if (NILP (Fget (new_symbol, Qevent_kind)))
  3185.     {
  3186.       Lisp_Object kind;
  3187.  
  3188.       kind = Fget (base, Qevent_kind);
  3189.       if (! NILP (kind))
  3190.     Fput (new_symbol, Qevent_kind, kind);
  3191.     }
  3192.  
  3193.   return new_symbol;
  3194. }
  3195.  
  3196.  
  3197. /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
  3198.    return a symbol with the modifiers placed in the canonical order.
  3199.    Canonical order is alphabetical, except for down and drag, which
  3200.    always come last.  The 'click' modifier is never written out.
  3201.  
  3202.    Fdefine_key calls this to make sure that (for example) C-M-foo
  3203.    and M-C-foo end up being equivalent in the keymap.  */
  3204.  
  3205. Lisp_Object
  3206. reorder_modifiers (symbol)
  3207.      Lisp_Object symbol;
  3208. {
  3209.   /* It's hopefully okay to write the code this way, since everything
  3210.      will soon be in caches, and no consing will be done at all.  */
  3211.   Lisp_Object parsed;
  3212.  
  3213.   parsed = parse_modifiers (symbol);
  3214.   return apply_modifiers (XCONS (XCONS (parsed)->cdr)->car,
  3215.               XCONS (parsed)->car);
  3216. }
  3217.  
  3218.  
  3219. /* For handling events, we often want to produce a symbol whose name
  3220.    is a series of modifier key prefixes ("M-", "C-", etcetera) attached
  3221.    to some base, like the name of a function key or mouse button.
  3222.    modify_event_symbol produces symbols of this sort.
  3223.  
  3224.    NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
  3225.    is the name of the i'th symbol.  TABLE_SIZE is the number of elements
  3226.    in the table.
  3227.  
  3228.    Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
  3229.    NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
  3230.  
  3231.    SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
  3232.    persist between calls to modify_event_symbol that it can use to
  3233.    store a cache of the symbols it's generated for this NAME_TABLE
  3234.    before.  The object stored there may be a vector or an alist.
  3235.  
  3236.    SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
  3237.    
  3238.    MODIFIERS is a set of modifier bits (as given in struct input_events)
  3239.    whose prefixes should be applied to the symbol name.
  3240.  
  3241.    SYMBOL_KIND is the value to be placed in the event_kind property of
  3242.    the returned symbol. 
  3243.  
  3244.    The symbols we create are supposed to have an
  3245.    `event-symbol-elements' property, which lists the modifiers present
  3246.    in the symbol's name.  */
  3247.  
  3248. static Lisp_Object
  3249. modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
  3250.                      name_table, symbol_table, table_size)
  3251.      int symbol_num;
  3252.      unsigned modifiers;
  3253.      Lisp_Object symbol_kind;
  3254.      Lisp_Object name_alist;
  3255.      char **name_table;
  3256.      Lisp_Object *symbol_table;
  3257.      int table_size;
  3258. {
  3259.   Lisp_Object value;
  3260.   Lisp_Object symbol_int;
  3261.  
  3262.   XSET (symbol_int, Lisp_Int, symbol_num);
  3263.  
  3264.   /* Is this a request for a valid symbol?  */
  3265.   if (symbol_num < 0 || symbol_num >= table_size)
  3266.     abort ();
  3267.  
  3268.   if (CONSP (*symbol_table))
  3269.     value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
  3270.  
  3271.   /* If *symbol_table doesn't seem to be initialized properly, fix that.
  3272.      *symbol_table should be a lisp vector TABLE_SIZE elements long,
  3273.      where the Nth element is the symbol for NAME_TABLE[N], or nil if
  3274.      we've never used that symbol before.  */
  3275.   else
  3276.     {
  3277.       if (! VECTORP (*symbol_table)
  3278.       || XVECTOR (*symbol_table)->size != table_size)
  3279.     {
  3280.       Lisp_Object size;
  3281.  
  3282.       XFASTINT (size) = table_size;
  3283.       *symbol_table = Fmake_vector (size, Qnil);
  3284.     }
  3285.  
  3286.       value = XVECTOR (*symbol_table)->contents[symbol_num];
  3287.     }
  3288.  
  3289.   /* Have we already used this symbol before?  */
  3290.   if (NILP (value))
  3291.     {
  3292.       /* No; let's create it.  */
  3293.       if (!NILP (name_alist))
  3294.     value = Fcdr_safe (Fassq (symbol_int, name_alist));
  3295.       else if (name_table[symbol_num])
  3296.     value = intern (name_table[symbol_num]);
  3297.  
  3298.       if (NILP (value))
  3299.     {
  3300.       char buf[20];
  3301.       sprintf (buf, "key-%d", symbol_num);
  3302.       value = intern (buf);
  3303.     }
  3304.  
  3305.       if (CONSP (*symbol_table))
  3306.     *symbol_table = Fcons (value, *symbol_table);
  3307.       else
  3308.     XVECTOR (*symbol_table)->contents[symbol_num] = value;
  3309.  
  3310.       /* Fill in the cache entries for this symbol; this also     
  3311.      builds the Qevent_symbol_elements property, which the user
  3312.      cares about.  */
  3313.       apply_modifiers (modifiers & click_modifier, value);
  3314.       Fput (value, Qevent_kind, symbol_kind);
  3315.     }
  3316.  
  3317.   /* Apply modifiers to that symbol.  */
  3318.   return apply_modifiers (modifiers, value);
  3319. }
  3320.  
  3321.  
  3322. /* Store into *addr a value nonzero if terminal input chars are available.
  3323.    Serves the purpose of ioctl (0, FIONREAD, addr)
  3324.    but works even if FIONREAD does not exist.
  3325.    (In fact, this may actually read some input.)  */
  3326.  
  3327. static void
  3328. get_input_pending (addr)
  3329.      int *addr;
  3330. {
  3331.   /* First of all, have we already counted some input?  */
  3332.   *addr = !NILP (Vquit_flag) || readable_events ();
  3333.  
  3334.   /* If input is being read as it arrives, and we have none, there is none.  */
  3335.   if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
  3336.     return;
  3337.  
  3338.   /* Try to read some input and see how much we get.  */
  3339.   gobble_input (0);
  3340.   *addr = !NILP (Vquit_flag) || readable_events ();
  3341. }
  3342.  
  3343. /* Interface to read_avail_input, blocking SIGIO if necessary.  */
  3344.  
  3345. int
  3346. gobble_input (expected)
  3347.      int expected;
  3348. {
  3349. #ifndef VMS
  3350. #ifdef SIGIO
  3351.   if (interrupt_input)
  3352.     {
  3353.       SIGMASKTYPE mask;
  3354.       mask = sigblockx (SIGIO);
  3355.       read_avail_input (expected);
  3356.       sigsetmask (mask);
  3357.     }
  3358.   else
  3359. #endif
  3360.     read_avail_input (expected);
  3361. #endif
  3362. }
  3363.  
  3364. /* Put a buffer_switch_event in the buffer
  3365.    so that read_key_sequence will notice the new current buffer.  */
  3366.  
  3367. record_asynch_buffer_change ()
  3368. {
  3369.   struct input_event event;
  3370.   event.kind = buffer_switch_event;
  3371.   event.frame_or_window = Qnil;
  3372.  
  3373.   /* Make sure no interrupt happens while storing the event.  */
  3374. #ifdef SIGIO
  3375.   if (interrupt_input)
  3376.     {
  3377.       SIGMASKTYPE mask;
  3378.       mask = sigblockx (SIGIO);
  3379.       kbd_buffer_store_event (&event);
  3380.       sigsetmask (mask);
  3381.     }
  3382.   else
  3383. #endif
  3384.     {
  3385.       stop_polling ();
  3386.       kbd_buffer_store_event (&event);
  3387.       start_polling ();
  3388.     }
  3389. }
  3390.  
  3391. #ifndef VMS
  3392.  
  3393. /* Read any terminal input already buffered up by the system
  3394.    into the kbd_buffer, but do not wait.
  3395.  
  3396.    EXPECTED should be nonzero if the caller knows there is some input.
  3397.  
  3398.    Except on VMS, all input is read by this function.
  3399.    If interrupt_input is nonzero, this function MUST be called
  3400.    only when SIGIO is blocked.
  3401.  
  3402.    Returns the number of keyboard chars read, or -1 meaning
  3403.    this is a bad time to try to read input.  */
  3404.  
  3405. static int
  3406. read_avail_input (expected)
  3407.      int expected;
  3408. {
  3409.   struct input_event buf[KBD_BUFFER_SIZE];
  3410.   register int i;
  3411.   int nread;
  3412.  
  3413.   if (read_socket_hook)
  3414.     /* No need for FIONREAD or fcntl; just say don't wait.  */
  3415.     nread = (*read_socket_hook) (0, buf, KBD_BUFFER_SIZE, expected, expected);
  3416.   else
  3417.     {
  3418.       /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
  3419.      the kbd_buffer can really hold.  That may prevent loss
  3420.      of characters on some systems when input is stuffed at us.  */
  3421.       unsigned char cbuf[KBD_BUFFER_SIZE - 1];
  3422.       int n_to_read;
  3423.  
  3424.       /* Determine how many characters we should *try* to read.  */
  3425. #ifdef MSDOS
  3426.       n_to_read = dos_keysns ();
  3427.       if (n_to_read == 0)
  3428.     return 0;
  3429. #else /* not MSDOS */
  3430. #ifdef FIONREAD
  3431.       /* Find out how much input is available.  */
  3432.       if (ioctl (0, FIONREAD, &n_to_read) < 0)
  3433.     /* Formerly simply reported no input, but that sometimes led to
  3434.        a failure of Emacs to terminate.
  3435.        SIGHUP seems appropriate if we can't reach the terminal.  */
  3436.     /* ??? Is it really right to send the signal just to this process
  3437.        rather than to the whole process group?
  3438.        Perhaps on systems with FIONREAD Emacs is alone in its group.  */
  3439.     kill (getpid (), SIGHUP);
  3440.       if (n_to_read == 0)
  3441.     return 0;
  3442.       if (n_to_read > sizeof cbuf)
  3443.     n_to_read = sizeof cbuf;
  3444. #else /* no FIONREAD */
  3445. #if defined(USG) || defined(DGUX)
  3446.       /* Read some input if available, but don't wait.  */
  3447.       n_to_read = sizeof cbuf;
  3448.       fcntl (fileno (stdin), F_SETFL, O_NDELAY);
  3449. #else
  3450. #ifdef    AMIGA    /* This is where the input work finally gets done */
  3451.   /* Note, The nread != 0 case isn't handled as it doesn't arise on the Amiga.
  3452.      (Look carefully at calls to read_avail_input) */
  3453.       assert(expected == 0); /* CHFIXME */
  3454.       n_to_read = sizeof(cbuf);
  3455. #else  /* not AMIGA */
  3456.       you lose;
  3457. #endif /* not AMIGA */
  3458. #endif
  3459. #endif
  3460. #endif /* not MSDOS */
  3461.  
  3462.       /* Now read; for one reason or another, this will not block.
  3463.      NREAD is set to the number of chars read.  */
  3464.       do
  3465.     {
  3466. #ifdef MSDOS
  3467.       cbuf[0] = dos_keyread();
  3468.       nread = 1;
  3469. #else
  3470.       nread = read (fileno (stdin), cbuf, n_to_read);
  3471. #endif
  3472. #if defined (AIX) && (! defined (aix386) && defined (_BSD))
  3473.       /* The kernel sometimes fails to deliver SIGHUP for ptys.
  3474.          This looks incorrect, but it isn't, because _BSD causes
  3475.          O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
  3476.          and that causes a value other than 0 when there is no input.  */
  3477.       if (nread == 0)
  3478.         kill (0, SIGHUP);
  3479. #endif
  3480.     }
  3481.       while (
  3482.          /* We used to retry the read if it was interrupted.
  3483.         But this does the wrong thing when O_NDELAY causes
  3484.         an EAGAIN error.  Does anybody know of a situation
  3485.         where a retry is actually needed?  */
  3486. #if 0
  3487.          nread < 0 && (errno == EAGAIN
  3488. #ifdef EFAULT
  3489.                || errno == EFAULT
  3490. #endif
  3491. #ifdef EBADSLT
  3492.                || errno == EBADSLT
  3493. #endif
  3494.                )
  3495. #else
  3496.          0
  3497. #endif
  3498.          );
  3499.  
  3500. #ifndef FIONREAD
  3501. #if defined (USG) || defined (DGUX)
  3502.       fcntl (fileno (stdin), F_SETFL, 0);
  3503. #endif /* USG or DGUX */
  3504. #endif /* no FIONREAD */
  3505.       for (i = 0; i < nread; i++)
  3506.     {
  3507.       buf[i].kind = ascii_keystroke;
  3508.       buf[i].modifiers = 0;
  3509.       if (meta_key == 1 && (cbuf[i] & 0x80))
  3510.         buf[i].modifiers = meta_modifier;
  3511.       if (meta_key != 2)
  3512.         cbuf[i] &= ~0x80;
  3513.         
  3514.       XSET (buf[i].code,        Lisp_Int,   cbuf[i]);
  3515. #ifdef MULTI_FRAME
  3516.       XSET (buf[i].frame_or_window, Lisp_Frame, selected_frame);
  3517. #else
  3518.       buf[i].frame_or_window = Qnil;
  3519. #endif
  3520.     }
  3521.     }
  3522.  
  3523.   /* Scan the chars for C-g and store them in kbd_buffer.  */
  3524.   for (i = 0; i < nread; i++)
  3525.     {
  3526.       kbd_buffer_store_event (&buf[i]);
  3527.       /* Don't look at input that follows a C-g too closely.
  3528.      This reduces lossage due to autorepeat on C-g.  */
  3529.       if (buf[i].kind == ascii_keystroke
  3530.       && XINT(buf[i].code) == quit_char)
  3531.     break;
  3532.     }
  3533.  
  3534.   return nread;
  3535. }
  3536. #endif /* not VMS */
  3537.  
  3538. #ifdef SIGIO   /* for entire page */
  3539. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  3540.  
  3541. SIGTYPE
  3542. input_available_signal (signo)
  3543.      int signo;
  3544. {
  3545.   /* Must preserve main program's value of errno.  */
  3546.   int old_errno = errno;
  3547. #ifdef BSD4_1
  3548.   extern int select_alarmed;
  3549. #endif
  3550.  
  3551. #ifdef USG
  3552.   /* USG systems forget handlers when they are used;
  3553.      must reestablish each time */
  3554.   signal (signo, input_available_signal);
  3555. #endif /* USG */
  3556.  
  3557. #ifdef BSD4_1
  3558.   sigisheld (SIGIO);
  3559. #endif
  3560.  
  3561.   if (input_available_clear_time)
  3562.     EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
  3563.  
  3564.   while (1)
  3565.     {
  3566.       int nread;
  3567.       nread = read_avail_input (1);
  3568.       /* -1 means it's not ok to read the input now.
  3569.      UNBLOCK_INPUT will read it later; now, avoid infinite loop.
  3570.      0 means there was no keyboard input available.  */
  3571.       if (nread <= 0)
  3572.     break;
  3573.  
  3574. #ifdef BSD4_1
  3575.       select_alarmed = 1;  /* Force the select emulator back to life */
  3576. #endif
  3577.     }
  3578.  
  3579. #ifdef BSD4_1
  3580.   sigfree ();
  3581. #endif
  3582.   errno = old_errno;
  3583. }
  3584. #endif /* SIGIO */
  3585.  
  3586. /* Send ourselves a SIGIO.
  3587.  
  3588.    This function exists so that the UNBLOCK_INPUT macro in
  3589.    blockinput.h can have some way to take care of input we put off
  3590.    dealing with, without assuming that every file which uses
  3591.    UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
  3592. void
  3593. reinvoke_input_signal ()
  3594. {
  3595. #ifdef SIGIO  
  3596.   kill (0, SIGIO);
  3597. #endif
  3598. }
  3599.  
  3600.  
  3601.  
  3602. /* Return the prompt-string of a sparse keymap.
  3603.    This is the first element which is a string.
  3604.    Return nil if there is none.  */
  3605.  
  3606. Lisp_Object
  3607. map_prompt (map)
  3608.      Lisp_Object map;
  3609. {
  3610.   while (CONSP (map))
  3611.     {
  3612.       register Lisp_Object tem;
  3613.       tem = Fcar (map);
  3614.       if (XTYPE (tem) == Lisp_String)
  3615.     return tem;
  3616.       map = Fcdr (map);
  3617.     }
  3618.   return Qnil;
  3619. }
  3620.  
  3621. static void menu_bar_item ();
  3622. static void menu_bar_one_keymap ();
  3623.  
  3624. /* These variables hold the vector under construction within
  3625.    menu_bar_items and its subroutines, and the current index
  3626.    for storing into that vector.  */
  3627. static Lisp_Object menu_bar_items_vector;
  3628. static Lisp_Object menu_bar_items_index;
  3629.  
  3630. /* Return a vector of menu items for a menu bar, appropriate
  3631.    to the current buffer.  Each item has three elements in the vector:
  3632.    KEY STRING MAPLIST.
  3633.  
  3634.    OLD is an old vector we can optionally reuse, or nil.  */
  3635.  
  3636. Lisp_Object
  3637. menu_bar_items (old)
  3638.      Lisp_Object old;
  3639. {
  3640.   /* The number of keymaps we're scanning right now, and the number of
  3641.      keymaps we have allocated space for.  */
  3642.   int nmaps;
  3643.  
  3644.   /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
  3645.      in the current keymaps, or nil where it is not a prefix.  */
  3646.   Lisp_Object *maps;
  3647.  
  3648.   Lisp_Object def, tem, tail;
  3649.  
  3650.   Lisp_Object result;
  3651.  
  3652.   int mapno;
  3653.   Lisp_Object oquit;
  3654.  
  3655.   int i;
  3656.  
  3657.   struct gcpro gcpro1;
  3658.  
  3659.   /* In order to build the menus, we need to call the keymap
  3660.      accessors.  They all call QUIT.  But this function is called
  3661.      during redisplay, during which a quit is fatal.  So inhibit
  3662.      quitting while building the menus.
  3663.      We do this instead of specbind because (1) errors will clear it anyway
  3664.      and (2) this avoids risk of specpdl overflow.  */
  3665.   oquit = Vinhibit_quit;
  3666.   Vinhibit_quit = Qt; 
  3667.  
  3668.   if (!NILP (old))
  3669.     menu_bar_items_vector = old;
  3670.   else
  3671.     menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
  3672.   menu_bar_items_index = 0;
  3673.  
  3674.   GCPRO1 (menu_bar_items_vector);
  3675.  
  3676.   /* Build our list of keymaps.
  3677.      If we recognize a function key and replace its escape sequence in
  3678.      keybuf with its symbol, or if the sequence starts with a mouse
  3679.      click and we need to switch buffers, we jump back here to rebuild
  3680.      the initial keymaps from the current buffer.  */
  3681.   { 
  3682.     Lisp_Object *tmaps;
  3683.  
  3684.     if (!NILP (Voverriding_local_map))
  3685.       {
  3686.     nmaps = 2;
  3687.     maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
  3688.     maps[0] = Voverriding_local_map;
  3689.       }
  3690.     else
  3691.       {
  3692.     nmaps = current_minor_maps (0, &tmaps) + 2;
  3693.     maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
  3694.     bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
  3695. #ifdef USE_TEXT_PROPERTIES
  3696.     maps[nmaps-2] = get_local_map (PT, current_buffer);
  3697. #else
  3698.     maps[nmaps-2] = current_buffer->keymap;
  3699. #endif
  3700.       }
  3701.     maps[nmaps-1] = current_global_map;
  3702.   }
  3703.  
  3704.   /* Look up in each map the dummy prefix key `menu-bar'.  */
  3705.  
  3706.   result = Qnil;
  3707.  
  3708.   for (mapno = nmaps - 1; mapno >= 0; mapno--)
  3709.     {
  3710.       if (! NILP (maps[mapno]))
  3711.     def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0), 0);
  3712.       else
  3713.     def = Qnil;
  3714.  
  3715.       tem = Fkeymapp (def);
  3716.       if (!NILP (tem))
  3717.     menu_bar_one_keymap (def);
  3718.     }
  3719.  
  3720.   /* Move to the end those items that should be at the end.  */
  3721.  
  3722.   for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
  3723.     {
  3724.       int i;
  3725.       int end = menu_bar_items_index;
  3726.  
  3727.       for (i = 0; i < end; i += 3)
  3728.     if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
  3729.       {
  3730.         Lisp_Object tem0, tem1, tem2;
  3731.         /* Move the item at index I to the end,
  3732.            shifting all the others forward.  */
  3733.         tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
  3734.         tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
  3735.         tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
  3736.         if (end > i + 3)
  3737.           bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
  3738.              &XVECTOR (menu_bar_items_vector)->contents[i],
  3739.              (end - i - 3) * sizeof (Lisp_Object));
  3740.         XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem0;
  3741.         XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem1;
  3742.         XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem2;
  3743.         break;
  3744.       }
  3745.     }
  3746.  
  3747.   /* Add nil, nil, nil at the end.  */
  3748.   i = menu_bar_items_index;
  3749.   if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
  3750.     {
  3751.       Lisp_Object tem;
  3752.       int newsize = 2 * i;
  3753.       tem = Fmake_vector (make_number (2 * i), Qnil);
  3754.       bcopy (XVECTOR (menu_bar_items_vector)->contents,
  3755.          XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
  3756.       menu_bar_items_vector = tem;
  3757.     }
  3758.   /* Add this item.  */
  3759.   XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
  3760.   XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
  3761.   XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
  3762.   menu_bar_items_index = i;
  3763.  
  3764.   Vinhibit_quit = oquit;
  3765.   UNGCPRO;
  3766.   return menu_bar_items_vector;
  3767. }
  3768.  
  3769. /* Scan one map KEYMAP, accumulating any menu items it defines
  3770.    in menu_bar_items_vector.  */
  3771.  
  3772. static void
  3773. menu_bar_one_keymap (keymap)
  3774.      Lisp_Object keymap;
  3775. {
  3776.   Lisp_Object tail, item, key, binding, item_string, table;
  3777.  
  3778.   /* Loop over all keymap entries that have menu strings.  */
  3779.   for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
  3780.     {
  3781.       item = XCONS (tail)->car;
  3782.       if (XTYPE (item) == Lisp_Cons)
  3783.     {
  3784.       key = XCONS (item)->car;
  3785.       binding = XCONS (item)->cdr;
  3786.       if (XTYPE (binding) == Lisp_Cons)
  3787.         {
  3788.           item_string = XCONS (binding)->car;
  3789.           if (XTYPE (item_string) == Lisp_String)
  3790.         menu_bar_item (key, item_string, Fcdr (binding));
  3791.         }
  3792.       else if (EQ (binding, Qundefined))
  3793.         menu_bar_item (key, Qnil, binding);
  3794.     }
  3795.       else if (XTYPE (item) == Lisp_Vector)
  3796.     {
  3797.       /* Loop over the char values represented in the vector.  */
  3798.       int len = XVECTOR (item)->size;
  3799.       int c;
  3800.       for (c = 0; c < len; c++)
  3801.         {
  3802.           Lisp_Object character;
  3803.           XFASTINT (character) = c;
  3804.           binding = XVECTOR (item)->contents[c];
  3805.           if (XTYPE (binding) == Lisp_Cons)
  3806.         {
  3807.           item_string = XCONS (binding)->car;
  3808.           if (XTYPE (item_string) == Lisp_String)
  3809.             menu_bar_item (key, item_string, Fcdr (binding));
  3810.         }
  3811.           else if (EQ (binding, Qundefined))
  3812.         menu_bar_item (key, Qnil, binding);
  3813.         }
  3814.     }
  3815.     }
  3816. }
  3817.  
  3818. /* This is used as the handler when calling internal_condition_case_1.  */
  3819.  
  3820. static Lisp_Object
  3821. menu_bar_item_1 (arg)
  3822.      Lisp_Object arg;
  3823. {
  3824.   return Qnil;
  3825. }
  3826.  
  3827. /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
  3828.    If there's already an item for KEY, add this DEF to it.  */
  3829.  
  3830. static void
  3831. menu_bar_item (key, item_string, def)
  3832.      Lisp_Object key, item_string, def;
  3833. {
  3834.   Lisp_Object tem;
  3835.   Lisp_Object enabled;
  3836.   int i;
  3837.  
  3838.   if (EQ (def, Qundefined))
  3839.     {
  3840.       /* If a map has an explicit `undefined' as definition,
  3841.      discard any previously made menu bar item.  */
  3842.  
  3843.       for (i = 0; i < menu_bar_items_index; i += 3)
  3844.     if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
  3845.       {
  3846.         if (menu_bar_items_index > i + 3)
  3847.           bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
  3848.              &XVECTOR (menu_bar_items_vector)->contents[i],
  3849.              (menu_bar_items_index - i - 3) * sizeof (Lisp_Object));
  3850.         menu_bar_items_index -= 3;
  3851.         return;
  3852.       }
  3853.  
  3854.       /* If there's no definition for this key yet,
  3855.      just ignore `undefined'.  */
  3856.       return;
  3857.     }
  3858.  
  3859.   /* See if this entry is enabled.  */
  3860.   enabled = Qt;
  3861.  
  3862.   if (XTYPE (def) == Lisp_Symbol)
  3863.     {
  3864.       /* No property, or nil, means enable.
  3865.      Otherwise, enable if value is not nil.  */
  3866.       tem = Fget (def, Qmenu_enable);
  3867.       if (!NILP (tem))
  3868.     /* (condition-case nil (eval tem)
  3869.          (error nil))  */
  3870.     enabled = internal_condition_case_1 (Feval, tem, Qerror,
  3871.                          menu_bar_item_1);
  3872.     }
  3873.  
  3874.   /* Ignore this item if it's not enabled.  */
  3875.   if (NILP (enabled))
  3876.     return;
  3877.  
  3878.   /* Find any existing item for this KEY.  */
  3879.   for (i = 0; i < menu_bar_items_index; i += 3)
  3880.     if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
  3881.       break;
  3882.  
  3883.   /* If we did not find this KEY, add it at the end.  */
  3884.   if (i == menu_bar_items_index)
  3885.     {
  3886.       /* If vector is too small, get a bigger one.  */
  3887.       if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
  3888.     {
  3889.       Lisp_Object tem;
  3890.       int newsize = 2 * i;
  3891.       tem = Fmake_vector (make_number (2 * i), Qnil);
  3892.       bcopy (XVECTOR (menu_bar_items_vector)->contents,
  3893.          XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
  3894.       menu_bar_items_vector = tem;
  3895.     }
  3896.       /* Add this item.  */
  3897.       XVECTOR (menu_bar_items_vector)->contents[i++] = key;
  3898.       XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
  3899.       XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
  3900.       menu_bar_items_index = i;
  3901.     }
  3902.   /* We did find an item for this KEY.  Add DEF to its list of maps.  */
  3903.   else
  3904.     {
  3905.       Lisp_Object old;
  3906.       old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
  3907.       XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
  3908.     }
  3909. }
  3910.  
  3911. /* Read a character using menus based on maps in the array MAPS.
  3912.    NMAPS is the length of MAPS.  Return nil if there are no menus in the maps.
  3913.    Return t if we displayed a menu but the user rejected it.
  3914.  
  3915.    PREV_EVENT is the previous input event, or nil if we are reading
  3916.    the first event of a key sequence.
  3917.  
  3918.    If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
  3919.    if we used a mouse menu to read the input, or zero otherwise.  If
  3920.    USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
  3921.  
  3922.    The prompting is done based on the prompt-string of the map
  3923.    and the strings associated with various map elements.  
  3924.  
  3925.    This can be done with X menus or with menus put in the minibuf.
  3926.    These are done in different ways, depending on how the input will be read.
  3927.    Menus using X are done after auto-saving in read-char, getting the input
  3928.    event from Fx_popup_menu; menus using the minibuf use read_char recursively
  3929.    and do auto-saving in the inner call of read_char. */
  3930.  
  3931. static Lisp_Object
  3932. read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
  3933.      int nmaps;
  3934.      Lisp_Object *maps;
  3935.      Lisp_Object prev_event;
  3936.      int *used_mouse_menu;
  3937. {
  3938.   int mapno;
  3939.   register Lisp_Object name;
  3940.   Lisp_Object rest, vector;
  3941.  
  3942.   if (used_mouse_menu)
  3943.     *used_mouse_menu = 0;
  3944.  
  3945.   /* Use local over global Menu maps */
  3946.  
  3947.   if (! menu_prompting)
  3948.     return Qnil;
  3949.  
  3950.   /* Get the menu name from the first map that has one (a prompt string).  */
  3951.   for (mapno = 0; mapno < nmaps; mapno++)
  3952.     {
  3953.       name = map_prompt (maps[mapno]);
  3954.       if (!NILP (name))
  3955.     break;
  3956.     }
  3957.  
  3958.   /* If we don't have any menus, just read a character normally.  */
  3959.   if (mapno >= nmaps)
  3960.     return Qnil;
  3961.  
  3962. #ifdef HAVE_X_WINDOWS
  3963. #ifdef HAVE_X_MENU
  3964.   /* If we got to this point via a mouse click,
  3965.      use a real menu for mouse selection.  */
  3966.   if (EVENT_HAS_PARAMETERS (prev_event))
  3967.     {
  3968.       /* Display the menu and get the selection.  */
  3969.       Lisp_Object *realmaps
  3970.     = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
  3971.       Lisp_Object value;
  3972.       int nmaps1 = 0;
  3973.  
  3974.       /* Use the maps that are not nil.  */
  3975.       for (mapno = 0; mapno < nmaps; mapno++)
  3976.     if (!NILP (maps[mapno]))
  3977.       realmaps[nmaps1++] = maps[mapno];
  3978.  
  3979.       value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
  3980.       if (CONSP (value))
  3981.     {
  3982.       /* If we got more than one event, put all but the first
  3983.          onto this list to be read later.
  3984.          Return just the first event now.  */
  3985.       Vunread_command_events
  3986.         = nconc2 (XCONS (value)->cdr, Vunread_command_events);
  3987.       value = XCONS (value)->car;
  3988.     }
  3989.       else if (NILP (value))
  3990.     value = Qt;
  3991.       if (used_mouse_menu)
  3992.     *used_mouse_menu = 1;
  3993.       return value;
  3994.     }
  3995. #endif /* HAVE_X_MENU */
  3996. #endif /* HAVE_X_WINDOWS */
  3997.   return Qnil ;
  3998. }
  3999.  
  4000. static Lisp_Object
  4001. read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
  4002.      int commandflag ;
  4003.      int nmaps;
  4004.      Lisp_Object *maps;
  4005. {
  4006.   int mapno;
  4007.   register Lisp_Object name;
  4008.   int nlength;
  4009.   int width = FRAME_WIDTH (selected_frame) - 4;
  4010.   char *menu = (char *) alloca (width + 4);
  4011.   int idx = -1;
  4012.   int nobindings = 1;
  4013.   Lisp_Object rest, vector;
  4014.  
  4015.   if (! menu_prompting)
  4016.     return Qnil;
  4017.  
  4018.   /* Get the menu name from the first map that has one (a prompt string).  */
  4019.   for (mapno = 0; mapno < nmaps; mapno++)
  4020.     {
  4021.       name = map_prompt (maps[mapno]);
  4022.       if (!NILP (name))
  4023.     break;
  4024.     }
  4025.  
  4026.   /* If we don't have any menus, just read a character normally.  */
  4027.   if (mapno >= nmaps)
  4028.     return Qnil;
  4029.  
  4030.   /* Prompt string always starts with map's prompt, and a space.  */
  4031.   strcpy (menu, XSTRING (name)->data);
  4032.   nlength = XSTRING (name)->size;
  4033.   menu[nlength++] = ':';
  4034.   menu[nlength++] = ' ';
  4035.   menu[nlength] = 0;
  4036.  
  4037.   /* Start prompting at start of first map.  */
  4038.   mapno = 0;
  4039.   rest = maps[mapno];
  4040.  
  4041.   /* Present the documented bindings, a line at a time.  */
  4042.   while (1)
  4043.     {
  4044.       int notfirst = 0;
  4045.       int i = nlength;
  4046.       Lisp_Object obj;
  4047.       int ch;
  4048.       int orig_defn_macro ;
  4049.  
  4050.       /* Loop over elements of map.  */
  4051.       while (i < width)
  4052.     {
  4053.       Lisp_Object s, elt;
  4054.  
  4055.       /* If reached end of map, start at beginning of next map.  */
  4056.       if (NILP (rest))
  4057.         {
  4058.           mapno++;
  4059.           /* At end of last map, wrap around to first map if just starting,
  4060.          or end this line if already have something on it.  */
  4061.           if (mapno == nmaps)
  4062.         {
  4063.           mapno = 0;
  4064.           if (notfirst || nobindings) break;
  4065.         }
  4066.           rest = maps[mapno];
  4067.         }
  4068.  
  4069.       /* Look at the next element of the map.  */
  4070.       if (idx >= 0)
  4071.         elt = XVECTOR (vector)->contents[idx];
  4072.       else
  4073.         elt = Fcar_safe (rest);
  4074.  
  4075.       if (idx < 0 && XTYPE (elt) == Lisp_Vector)
  4076.         {
  4077.           /* If we found a dense table in the keymap,
  4078.          advanced past it, but start scanning its contents.  */
  4079.           rest = Fcdr_safe (rest);
  4080.           vector = elt;
  4081.           idx = 0;
  4082.         }
  4083.       else
  4084.         {
  4085.           /* An ordinary element.  */
  4086.           if ( idx < 0 )
  4087.         s = Fcar_safe (Fcdr_safe (elt));    /* alist */
  4088.           else
  4089.         s = Fcar_safe(elt);            /* vector */
  4090.           if (XTYPE (s) != Lisp_String)
  4091.         /* Ignore the element if it has no prompt string.  */
  4092.         ;
  4093.           /* If we have room for the prompt string, add it to this line.
  4094.          If this is the first on the line, always add it.  */
  4095.           else if (XSTRING (s)->size + i + 2 < width
  4096.                || !notfirst)
  4097.         {
  4098.           int thiswidth;
  4099.  
  4100.           /* Punctuate between strings.  */
  4101.           if (notfirst)
  4102.             {
  4103.               strcpy (menu + i, ", ");
  4104.               i += 2;
  4105.             }
  4106.           notfirst = 1;
  4107.           nobindings = 0 ;
  4108.  
  4109.           /* Add as much of string as fits.  */
  4110.           thiswidth = XSTRING (s)->size;
  4111.           if (thiswidth + i > width)
  4112.             thiswidth = width - i;
  4113.           bcopy (XSTRING (s)->data, menu + i, thiswidth);
  4114.           i += thiswidth;
  4115.           menu[i] = 0;
  4116.         }
  4117.           else
  4118.         {
  4119.           /* If this element does not fit, end the line now,
  4120.              and save the element for the next line.  */
  4121.           strcpy (menu + i, "...");
  4122.           break;
  4123.         }
  4124.  
  4125.           /* Move past this element.  */
  4126.           if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
  4127.         /* Handle reaching end of dense table.  */
  4128.         idx = -1;
  4129.           if (idx >= 0)
  4130.         idx++;
  4131.           else
  4132.         rest = Fcdr_safe (rest);
  4133.         }
  4134.     }
  4135.  
  4136.       /* Prompt with that and read response.  */
  4137.       message1 (menu);
  4138.  
  4139.       /* Make believe its not a keyboard macro in case the help char 
  4140.      is pressed.  Help characters are not recorded because menu prompting
  4141.      is not used on replay.
  4142.      */
  4143.       orig_defn_macro = defining_kbd_macro ;
  4144.       defining_kbd_macro = 0 ;
  4145.       do
  4146.     obj = read_char (commandflag, 0, 0, Qnil, 0);
  4147.       while (XTYPE (obj) == Lisp_Buffer);
  4148.       defining_kbd_macro = orig_defn_macro ;
  4149.  
  4150.       if (XTYPE (obj) != Lisp_Int)
  4151.     return obj;
  4152.       else
  4153.     ch = XINT (obj);
  4154.  
  4155.       if (! EQ (obj, menu_prompt_more_char)
  4156.       && (XTYPE (menu_prompt_more_char) != Lisp_Int
  4157.           || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
  4158.     {
  4159.       if ( defining_kbd_macro )
  4160.         store_kbd_macro_char(obj) ;
  4161.       return obj;
  4162.     }
  4163.       /* Help char - go round again */
  4164.     }
  4165. }
  4166.  
  4167. /* Reading key sequences.  */
  4168.  
  4169. /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
  4170.    in DEFS[0..NMAPS-1].  Set NEXT[i] to DEFS[i] if DEFS[i] is a
  4171.    keymap, or nil otherwise.  Return the index of the first keymap in
  4172.    which KEY has any binding, or NMAPS if no map has a binding.
  4173.  
  4174.    If KEY is a meta ASCII character, treat it like meta-prefix-char
  4175.    followed by the corresponding non-meta character.  Keymaps in
  4176.    CURRENT with non-prefix bindings for meta-prefix-char become nil in
  4177.    NEXT.
  4178.  
  4179.    When KEY is not defined in any of the keymaps, if it is an upper
  4180.    case letter and there are bindings for the corresponding lower-case
  4181.    letter, return the bindings for the lower-case letter.
  4182.    We store 1 in *CASE_CONVERTED in this case.
  4183.    Otherwise, we don't change *CASE_CONVERTED.
  4184.  
  4185.    If KEY has no bindings in any of the CURRENT maps, NEXT is left
  4186.    unmodified.
  4187.  
  4188.    NEXT may == CURRENT.  */
  4189.  
  4190. static int
  4191. follow_key (key, nmaps, current, defs, next, case_converted)
  4192.      Lisp_Object key;
  4193.      Lisp_Object *current, *defs, *next;
  4194.      int nmaps;
  4195.      int *case_converted;
  4196. {
  4197.   int i, first_binding;
  4198.  
  4199.   /* If KEY is a meta ASCII character, treat it like meta-prefix-char
  4200.      followed by the corresponding non-meta character.  */
  4201.   if (XTYPE (key) == Lisp_Int && (XINT (key) & CHAR_META))
  4202.     {
  4203.       for (i = 0; i < nmaps; i++)
  4204.     if (! NILP (current[i]))
  4205.       {
  4206.         next[i] =
  4207.           get_keyelt (access_keymap (current[i], meta_prefix_char, 1, 0), 0);
  4208.  
  4209.         /* Note that since we pass the resulting bindings through
  4210.            get_keymap_1, non-prefix bindings for meta-prefix-char
  4211.            disappear.  */
  4212.         next[i] = get_keymap_1 (next[i], 0, 1);
  4213.       }
  4214.     else
  4215.       next[i] = Qnil;
  4216.  
  4217.       current = next;
  4218.       XSET (key, Lisp_Int, XFASTINT (key) & ~CHAR_META);
  4219.     }
  4220.  
  4221.   first_binding = nmaps;
  4222.   for (i = nmaps - 1; i >= 0; i--)
  4223.     {
  4224.       if (! NILP (current[i]))
  4225.     {
  4226.       defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0), 0);
  4227.       if (! NILP (defs[i]))
  4228.         first_binding = i;
  4229.     }
  4230.       else
  4231.     defs[i] = Qnil;
  4232.     }
  4233.  
  4234.   /* When KEY is not defined in any of the keymaps, if it is an upper
  4235.      case letter and there are bindings for the corresponding
  4236.      lower-case letter, return the bindings for the lower-case letter.  */
  4237.   if (first_binding == nmaps
  4238.       && XTYPE (key) == Lisp_Int
  4239.       && ((((XINT (key) & 0x3ffff)
  4240.         < XSTRING (current_buffer->downcase_table)->size)
  4241.        && UPPERCASEP (XINT (key) & 0x3ffff))
  4242.       || (XINT (key) & shift_modifier)))
  4243.     {
  4244.       if (XINT (key) & shift_modifier)
  4245.     XSETINT (key, XINT (key) & ~shift_modifier);
  4246.       else
  4247.     XSETINT (key, (DOWNCASE (XINT (key) & 0x3ffff)
  4248.                | (XINT (key) & ~0x3ffff)));
  4249.  
  4250.       first_binding = nmaps;
  4251.       for (i = nmaps - 1; i >= 0; i--)
  4252.     {
  4253.       if (! NILP (current[i]))
  4254.         {
  4255.           defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0), 0);
  4256.           if (! NILP (defs[i]))
  4257.         first_binding = i;
  4258.         }
  4259.       else
  4260.         defs[i] = Qnil;
  4261.     }
  4262.       if (first_binding  != nmaps)
  4263.     *case_converted = 1;
  4264.     }
  4265.  
  4266.   /* Given the set of bindings we've found, produce the next set of maps.  */
  4267.   if (first_binding < nmaps)
  4268.     for (i = 0; i < nmaps; i++)
  4269.       next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
  4270.  
  4271.   return first_binding;
  4272. }
  4273.  
  4274. /* Read a sequence of keys that ends with a non prefix character, 
  4275.    storing it in KEYBUF, a buffer of size BUFSIZE.
  4276.    Prompt with PROMPT.
  4277.    Return the length of the key sequence stored.
  4278.    Return -1 if the user rejected a command menu.
  4279.  
  4280.    Echo starting immediately unless `prompt' is 0.
  4281.  
  4282.    Where a key sequence ends depends on the currently active keymaps.
  4283.    These include any minor mode keymaps active in the current buffer,
  4284.    the current buffer's local map, and the global map.
  4285.  
  4286.    If a key sequence has no other bindings, we check Vfunction_key_map
  4287.    to see if some trailing subsequence might be the beginning of a
  4288.    function key's sequence.  If so, we try to read the whole function
  4289.    key, and substitute its symbolic name into the key sequence.
  4290.  
  4291.    We ignore unbound `down-' mouse clicks.  We turn unbound `drag-' and
  4292.    `double-' events into similar click events, if that would make them
  4293.    bound.  We try to turn `triple-' events first into `double-' events,
  4294.    then into clicks.
  4295.  
  4296.    If we get a mouse click in a mode line, vertical divider, or other
  4297.    non-text area, we treat the click as if it were prefixed by the
  4298.    symbol denoting that area - `mode-line', `vertical-line', or
  4299.    whatever.
  4300.  
  4301.    If the sequence starts with a mouse click, we read the key sequence
  4302.    with respect to the buffer clicked on, not the current buffer.
  4303.  
  4304.    If the user switches frames in the midst of a key sequence, we put
  4305.    off the switch-frame event until later; the next call to
  4306.    read_char will return it.  */
  4307.  
  4308. static int
  4309. read_key_sequence (keybuf, bufsize, prompt)
  4310.      Lisp_Object *keybuf;
  4311.      int bufsize;
  4312.      Lisp_Object prompt;
  4313. {
  4314.   int count = specpdl_ptr - specpdl;
  4315.  
  4316.   /* How many keys there are in the current key sequence.  */
  4317.   int t;
  4318.  
  4319.   /* The length of the echo buffer when we started reading, and
  4320.      the length of this_command_keys when we started reading.  */
  4321.   int echo_start;
  4322.   int keys_start;
  4323.  
  4324.   /* The number of keymaps we're scanning right now, and the number of
  4325.      keymaps we have allocated space for.  */
  4326.   int nmaps;
  4327.   int nmaps_allocated = 0;
  4328.  
  4329.   /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
  4330.      the current keymaps.  */
  4331.   Lisp_Object *defs;
  4332.  
  4333.   /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
  4334.      in the current keymaps, or nil where it is not a prefix.  */
  4335.   Lisp_Object *submaps;
  4336.  
  4337.   /* The index in defs[] of the first keymap that has a binding for
  4338.      this key sequence.  In other words, the lowest i such that
  4339.      defs[i] is non-nil.  */
  4340.   int first_binding;
  4341.  
  4342.   /* If t < mock_input, then KEYBUF[t] should be read as the next
  4343.      input key.
  4344.  
  4345.      We use this to recover after recognizing a function key.  Once we
  4346.      realize that a suffix of the current key sequence is actually a
  4347.      function key's escape sequence, we replace the suffix with the
  4348.      function key's binding from Vfunction_key_map.  Now keybuf
  4349.      contains a new and different key sequence, so the echo area,
  4350.      this_command_keys, and the submaps and defs arrays are wrong.  In
  4351.      this situation, we set mock_input to t, set t to 0, and jump to
  4352.      restart_sequence; the loop will read keys from keybuf up until
  4353.      mock_input, thus rebuilding the state; and then it will resume
  4354.      reading characters from the keyboard.  */
  4355.   int mock_input = 0;
  4356.  
  4357.   /* If the sequence is unbound in submaps[], then
  4358.      keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
  4359.      and fkey_map is its binding.
  4360.  
  4361.      These might be > t, indicating that all function key scanning
  4362.      should hold off until t reaches them.  We do this when we've just
  4363.      recognized a function key, to avoid searching for the function
  4364.      key's again in Vfunction_key_map.  */
  4365.   int fkey_start = 0, fkey_end = 0;
  4366.   Lisp_Object fkey_map;
  4367.  
  4368.   /* Likewise, for key_translation_map.  */
  4369.   int keytran_start = 0, keytran_end = 0;
  4370.   Lisp_Object keytran_map;
  4371.  
  4372.   /* If we receive a ``switch-frame'' event in the middle of a key sequence,
  4373.      we put it off for later.  While we're reading, we keep the event here.  */
  4374.   Lisp_Object delayed_switch_frame;
  4375.  
  4376.   /* See the comment below... */
  4377. #if defined (GOBBLE_FIRST_EVENT)
  4378.   Lisp_Object first_event;
  4379. #endif
  4380.  
  4381.   struct buffer *starting_buffer;
  4382.  
  4383.   /* Nonzero if we found the binding for one of the chars
  4384.      in this key sequence by downcasing it.  */
  4385.   int case_converted = 0;
  4386.  
  4387.   /* Nonzero if we seem to have got the beginning of a binding
  4388.      in function_key_map.  */
  4389.   int function_key_possible = 0;
  4390.  
  4391.   int junk;
  4392.  
  4393.   last_nonmenu_event = Qnil;
  4394.  
  4395.   delayed_switch_frame = Qnil;
  4396.   fkey_map = Vfunction_key_map;
  4397.   keytran_map = Vkey_translation_map;
  4398.  
  4399.   /* If there is no function-key-map, turn off function key scanning.  */
  4400.   if (NILP (Fkeymapp (Vfunction_key_map)))
  4401.     fkey_start = fkey_end = bufsize + 1;
  4402.  
  4403.   /* If there is no key-translation-map, turn off scanning.  */
  4404.   if (NILP (Fkeymapp (Vkey_translation_map)))
  4405.     keytran_start = keytran_end = bufsize + 1;
  4406.  
  4407.   if (INTERACTIVE)
  4408.     {
  4409.       if (!NILP (prompt))
  4410.     echo_prompt (XSTRING (prompt)->data);
  4411.       else if (cursor_in_echo_area)
  4412.     /* This doesn't put in a dash if the echo buffer is empty, so
  4413.        you don't always see a dash hanging out in the minibuffer.  */
  4414.     echo_dash ();
  4415.     }
  4416.  
  4417.   /* Record the initial state of the echo area and this_command_keys;
  4418.      we will need to restore them if we replay a key sequence.  */
  4419.   if (INTERACTIVE)
  4420.     echo_start = echo_length ();
  4421.   keys_start = this_command_key_count;
  4422.  
  4423. #if defined (GOBBLE_FIRST_EVENT)
  4424.   /* This doesn't quite work, because some of the things that read_char
  4425.      does cannot safely be bypassed.  It seems too risky to try to make
  4426.      this work right.  */ 
  4427.  
  4428.   /* Read the first char of the sequence specially, before setting
  4429.      up any keymaps, in case a filter runs and switches buffers on us.  */
  4430.   first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
  4431.                &junk);
  4432. #endif /* GOBBLE_FIRST_EVENT */
  4433.  
  4434.   /* We jump here when the key sequence has been thoroughly changed, and
  4435.      we need to rescan it starting from the beginning.  When we jump here,
  4436.      keybuf[0..mock_input] holds the sequence we should reread.  */
  4437.  replay_sequence:
  4438.  
  4439.   starting_buffer = current_buffer;
  4440.   case_converted = 0;
  4441.   function_key_possible = 0;
  4442.  
  4443.   /* Build our list of keymaps.
  4444.      If we recognize a function key and replace its escape sequence in
  4445.      keybuf with its symbol, or if the sequence starts with a mouse
  4446.      click and we need to switch buffers, we jump back here to rebuild
  4447.      the initial keymaps from the current buffer.  */
  4448.   { 
  4449.     Lisp_Object *maps;
  4450.  
  4451.     if (!NILP (Voverriding_local_map))
  4452.       {
  4453.     nmaps = 2;
  4454.     if (nmaps > nmaps_allocated)
  4455.       {
  4456.         submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
  4457.         defs    = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
  4458.         nmaps_allocated = nmaps;
  4459.       }
  4460.     submaps[0] = Voverriding_local_map;
  4461.       }
  4462.     else
  4463.       {
  4464.     nmaps = current_minor_maps (0, &maps) + 2;
  4465.     if (nmaps > nmaps_allocated)
  4466.       {
  4467.         submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
  4468.         defs    = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
  4469.         nmaps_allocated = nmaps;
  4470.       }
  4471.     bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
  4472. #ifdef USE_TEXT_PROPERTIES
  4473.     submaps[nmaps-2] = get_local_map (PT, current_buffer);
  4474. #else
  4475.     submaps[nmaps-2] = current_buffer->keymap;
  4476. #endif
  4477.       }
  4478.     submaps[nmaps-1] = current_global_map;
  4479.   }
  4480.  
  4481.   /* Find an accurate initial value for first_binding.  */
  4482.   for (first_binding = 0; first_binding < nmaps; first_binding++)
  4483.     if (! NILP (submaps[first_binding]))
  4484.       break;
  4485.  
  4486.   /* Start from the beginning in keybuf.  */
  4487.   t = 0;
  4488.  
  4489.   /* These are no-ops the first time through, but if we restart, they
  4490.      revert the echo area and this_command_keys to their original state.  */
  4491.   this_command_key_count = keys_start;
  4492.   if (INTERACTIVE && t < mock_input)
  4493.     echo_truncate (echo_start);
  4494.  
  4495.   /* If the best binding for the current key sequence is a keymap, or
  4496.      we may be looking at a function key's escape sequence, keep on
  4497.      reading.  */
  4498.   while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
  4499.      || (first_binding >= nmaps
  4500.          && fkey_start < t
  4501.          /* mock input is never part of a function key's sequence.  */
  4502.          && mock_input <= fkey_start)
  4503.      || (first_binding >= nmaps
  4504.          && keytran_start < t
  4505.          /* mock input is never part of a function key's sequence.  */
  4506.          && mock_input <= keytran_start)
  4507.      /* Don't return in the middle of a possible function key sequence,
  4508.         if the only bindings we found were via case conversion.
  4509.         Thus, if ESC O a has a function-key-map translation
  4510.         and ESC o has a binding, don't return after ESC O,
  4511.         so that we can translate ESC O plus the next character.  */
  4512.      || (function_key_possible && case_converted))
  4513.     {
  4514.       Lisp_Object key;
  4515.       int used_mouse_menu = 0;
  4516.  
  4517.       /* Where the last real key started.  If we need to throw away a
  4518.          key that has expanded into more than one element of keybuf
  4519.          (say, a mouse click on the mode line which is being treated
  4520.          as [mode-line (mouse-...)], then we backtrack to this point
  4521.          of keybuf.  */
  4522.       int last_real_key_start;
  4523.  
  4524.       /* These variables are analogous to echo_start and keys_start;
  4525.      while those allow us to restart the entire key sequence,
  4526.      echo_local_start and keys_local_start allow us to throw away
  4527.      just one key.  */
  4528.       int echo_local_start, keys_local_start, local_first_binding;
  4529.  
  4530.       if (t >= bufsize)
  4531.     error ("key sequence too long");
  4532.  
  4533.       if (INTERACTIVE)
  4534.     echo_local_start = echo_length ();
  4535.       keys_local_start = this_command_key_count;
  4536.       local_first_binding = first_binding;
  4537.       
  4538.     replay_key:
  4539.       /* These are no-ops, unless we throw away a keystroke below and
  4540.      jumped back up to replay_key; in that case, these restore the
  4541.      variables to their original state, allowing us to replay the
  4542.      loop.  */
  4543.       if (INTERACTIVE && t < mock_input)
  4544.     echo_truncate (echo_local_start);
  4545.       this_command_key_count = keys_local_start;
  4546.       first_binding = local_first_binding;
  4547.  
  4548.       /* By default, assume each event is "real".  */
  4549.       last_real_key_start = t;
  4550.  
  4551.       /* Does mock_input indicate that we are re-reading a key sequence?  */
  4552.       if (t < mock_input)
  4553.     {
  4554.       key = keybuf[t];
  4555.       add_command_key (key);
  4556.       echo_char (key);
  4557.     }
  4558.  
  4559.       /* If not, we should actually read a character.  */
  4560.       else
  4561.     {
  4562.       struct buffer *buf = current_buffer;
  4563.  
  4564.       key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
  4565.                &used_mouse_menu);
  4566.  
  4567.       /* read_char returns t when it shows a menu and the user rejects it.
  4568.          Just return -1.  */
  4569.       if (EQ (key, Qt))
  4570.         return -1;
  4571.  
  4572.       /* read_char returns -1 at the end of a macro.
  4573.          Emacs 18 handles this by returning immediately with a
  4574.          zero, so that's what we'll do.  */
  4575.       if (XTYPE (key) == Lisp_Int && XINT (key) == -1)
  4576.         {
  4577.           t = 0;
  4578.           goto done;
  4579.         }
  4580.       
  4581.       /* If the current buffer has been changed from under us, the
  4582.          keymap may have changed, so replay the sequence.  */
  4583.       if (XTYPE (key) == Lisp_Buffer)
  4584.         {
  4585.           mock_input = t;
  4586.           goto replay_sequence;
  4587.         }
  4588.  
  4589.       /* If we have a quit that was typed in another frame, and
  4590.          quit_throw_to_read_char switched buffers,
  4591.          replay to get the right keymap.  */
  4592.       if (EQ (key, quit_char) && current_buffer != starting_buffer)
  4593.         {
  4594.           keybuf[t++] = key;
  4595.           mock_input = t;
  4596.           Vquit_flag = Qnil;
  4597.           goto replay_sequence;
  4598.         }
  4599.  
  4600.       Vquit_flag = Qnil;
  4601.     }
  4602.  
  4603.       /* Clicks in non-text areas get prefixed by the symbol 
  4604.      in their CHAR-ADDRESS field.  For example, a click on
  4605.      the mode line is prefixed by the symbol `mode-line'.
  4606.  
  4607.      Furthermore, key sequences beginning with mouse clicks
  4608.      are read using the keymaps of the buffer clicked on, not
  4609.      the current buffer.  So we may have to switch the buffer
  4610.      here.
  4611.  
  4612.      When we turn one event into two events, we must make sure
  4613.      that neither of the two looks like the original--so that,
  4614.      if we replay the events, they won't be expanded again.
  4615.      If not for this, such reexpansion could happen either here
  4616.      or when user programs play with this-command-keys.  */
  4617.       if (EVENT_HAS_PARAMETERS (key))
  4618.     {
  4619.       Lisp_Object kind;
  4620.  
  4621.       kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
  4622.       if (EQ (kind, Qmouse_click))
  4623.         {
  4624.           Lisp_Object window, posn;
  4625.  
  4626.           window = POSN_WINDOW      (EVENT_START (key));
  4627.           posn   = POSN_BUFFER_POSN (EVENT_START (key));
  4628.           if (XTYPE (posn) == Lisp_Cons)
  4629.         {
  4630.           /* We're looking at the second event of a
  4631.              sequence which we expanded before.  Set
  4632.              last_real_key_start appropriately.  */
  4633.           if (t > 0)
  4634.             last_real_key_start = t - 1;
  4635.         }
  4636.  
  4637.           /* Key sequences beginning with mouse clicks are
  4638.          read using the keymaps in the buffer clicked on,
  4639.          not the current buffer.  If we're at the
  4640.          beginning of a key sequence, switch buffers.  */
  4641.           if (last_real_key_start == 0
  4642.           && XTYPE (window) == Lisp_Window
  4643.           && XTYPE (XWINDOW (window)->buffer) == Lisp_Buffer
  4644.           && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
  4645.         {
  4646.           keybuf[t] = key;
  4647.           mock_input = t + 1;
  4648.  
  4649.           /* Arrange to go back to the original buffer once we're
  4650.              done reading the key sequence.  Note that we can't
  4651.              use save_excursion_{save,restore} here, because they
  4652.              save point as well as the current buffer; we don't
  4653.              want to save point, because redisplay may change it,
  4654.              to accommodate a Fset_window_start or something.  We
  4655.              don't want to do this at the top of the function,
  4656.              because we may get input from a subprocess which
  4657.              wants to change the selected window and stuff (say,
  4658.              emacsclient).  */
  4659.           record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  4660.  
  4661.           set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
  4662.           goto replay_sequence;
  4663.         }
  4664.           else if (XTYPE (posn) == Lisp_Symbol)
  4665.         {
  4666.           /* Expand mode-line and scroll-bar events into two events:
  4667.              use posn as a fake prefix key.  */
  4668.  
  4669.           if (t + 1 >= bufsize)
  4670.             error ("key sequence too long");
  4671.           keybuf[t] = posn;
  4672.           keybuf[t+1] = key;
  4673.           mock_input = t + 2;
  4674.  
  4675.           /* Zap the position in key, so we know that we've
  4676.              expanded it, and don't try to do so again.  */
  4677.           POSN_BUFFER_POSN (EVENT_START (key))
  4678.             = Fcons (posn, Qnil);
  4679.           goto replay_key;
  4680.         }
  4681.         }
  4682.       else if (EQ (kind, Qswitch_frame))
  4683.         {
  4684.           /* If we're at the beginning of a key sequence, go
  4685.          ahead and return this event.  If we're in the
  4686.          midst of a key sequence, delay it until the end. */
  4687.           if (t > 0)
  4688.         {
  4689.           delayed_switch_frame = key;
  4690.           goto replay_key;
  4691.         }
  4692.         }
  4693.       else
  4694.         {
  4695.           Lisp_Object posn;
  4696.  
  4697.           posn = POSN_BUFFER_POSN (EVENT_START (key));
  4698.           /* Handle menu-bar events:
  4699.          insert the dummy prefix event `menu-bar'.  */
  4700.           if (EQ (posn, Qmenu_bar))
  4701.         {
  4702.           if (t + 1 >= bufsize)
  4703.             error ("key sequence too long");
  4704.           /* Run the Lucid hook.  */
  4705.           if (!NILP (Vrun_hooks))
  4706.             call1 (Vrun_hooks, Qactivate_menubar_hook);
  4707.           /* If it has changed current-menubar from previous value,
  4708.              really recompute the menubar from the value.  */
  4709.           if (! NILP (Vlucid_menu_bar_dirty_flag))
  4710.             call0 (Qrecompute_lucid_menubar);
  4711.           keybuf[t] = posn;
  4712.           keybuf[t+1] = key;
  4713.  
  4714.           /* Zap the position in key, so we know that we've
  4715.              expanded it, and don't try to do so again.  */
  4716.           POSN_BUFFER_POSN (EVENT_START (key))
  4717.             = Fcons (posn, Qnil);
  4718.  
  4719.           mock_input = t + 2;
  4720.           goto replay_sequence;
  4721.         }
  4722.           else if (XTYPE (posn) == Lisp_Cons)
  4723.         {
  4724.           /* We're looking at the second event of a
  4725.              sequence which we expanded before.  Set
  4726.              last_real_key_start appropriately.  */
  4727.           if (last_real_key_start == t && t > 0)
  4728.             last_real_key_start = t - 1;
  4729.         }
  4730.         }
  4731.     }
  4732.  
  4733.       /* We have finally decided that KEY is something we might want
  4734.      to look up.  */
  4735.       first_binding = (follow_key (key,
  4736.                    nmaps   - first_binding,
  4737.                    submaps + first_binding,
  4738.                    defs    + first_binding,
  4739.                    submaps + first_binding,
  4740.                    &case_converted)
  4741.                + first_binding);
  4742.  
  4743.       /* If KEY wasn't bound, we'll try some fallbacks.  */
  4744.       if (first_binding >= nmaps)
  4745.     {
  4746.       Lisp_Object head;
  4747.  
  4748.       head = EVENT_HEAD (key);
  4749.       if (EQ (head, Vhelp_char))
  4750.         {
  4751.           read_key_sequence_cmd = Vprefix_help_command;
  4752.           keybuf[t++] = key;
  4753.           last_nonmenu_event = key;
  4754.           goto done;
  4755.         }
  4756.  
  4757.       if (XTYPE (head) == Lisp_Symbol)
  4758.         {
  4759.           Lisp_Object breakdown;
  4760.           int modifiers;
  4761.  
  4762.           breakdown = parse_modifiers (head);
  4763.           modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
  4764.           /* Attempt to reduce an unbound mouse event to a simpler
  4765.          event that is bound:
  4766.            Drags reduce to clicks.
  4767.            Double-clicks reduce to clicks.
  4768.            Triple-clicks reduce to double-clicks, then to clicks.
  4769.            Down-clicks are eliminated.
  4770.            Double-downs reduce to downs, then are eliminated.
  4771.            Triple-downs reduce to double-downs, then to downs,
  4772.              then are eliminated. */
  4773.           if (modifiers & (down_modifier | drag_modifier
  4774.                    | double_modifier | triple_modifier))
  4775.         {
  4776.           while (modifiers & (down_modifier | drag_modifier
  4777.                       | double_modifier | triple_modifier))
  4778.             {
  4779.               Lisp_Object new_head, new_click;
  4780.               if (modifiers & triple_modifier)
  4781.             modifiers ^= (double_modifier | triple_modifier);
  4782.               else if (modifiers & (drag_modifier | double_modifier))
  4783.             modifiers &= ~(drag_modifier | double_modifier);
  4784.               else
  4785.             {
  4786.               /* Dispose of this `down' event by simply jumping
  4787.                  back to replay_key, to get another event.
  4788.  
  4789.                  Note that if this event came from mock input,
  4790.                  then just jumping back to replay_key will just
  4791.                  hand it to us again.  So we have to wipe out any
  4792.                  mock input.
  4793.  
  4794.                  We could delete keybuf[t] and shift everything
  4795.                  after that to the left by one spot, but we'd also
  4796.                  have to fix up any variable that points into
  4797.                  keybuf, and shifting isn't really necessary
  4798.                  anyway.
  4799.  
  4800.                  Adding prefixes for non-textual mouse clicks
  4801.                  creates two characters of mock input, and both
  4802.                  must be thrown away.  If we're only looking at
  4803.                  the prefix now, we can just jump back to
  4804.                  replay_key.  On the other hand, if we've already
  4805.                  processed the prefix, and now the actual click
  4806.                  itself is giving us trouble, then we've lost the
  4807.                  state of the keymaps we want to backtrack to, and
  4808.                  we need to replay the whole sequence to rebuild
  4809.                  it.
  4810.  
  4811.                  Beyond that, only function key expansion could
  4812.                  create more than two keys, but that should never
  4813.                  generate mouse events, so it's okay to zero
  4814.                  mock_input in that case too.
  4815.  
  4816.                  Isn't this just the most wonderful code ever?  */
  4817.               if (t == last_real_key_start)
  4818.                 {
  4819.                   mock_input = 0;
  4820.                   goto replay_key;
  4821.                 }
  4822.               else
  4823.                 {
  4824.                   mock_input = last_real_key_start;
  4825.                   goto replay_sequence;
  4826.                 }
  4827.             }
  4828.  
  4829.               new_head
  4830.             = apply_modifiers (modifiers, XCONS (breakdown)->car);
  4831.               new_click
  4832.             = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
  4833.  
  4834.               /* Look for a binding for this new key.  follow_key
  4835.              promises that it didn't munge submaps the
  4836.              last time we called it, since key was unbound.  */
  4837.               first_binding
  4838.             = (follow_key (new_click,
  4839.                        nmaps   - local_first_binding,
  4840.                        submaps + local_first_binding,
  4841.                        defs    + local_first_binding,
  4842.                        submaps + local_first_binding,
  4843.                        &case_converted)
  4844.                + local_first_binding);
  4845.  
  4846.               /* If that click is bound, go for it.  */
  4847.               if (first_binding < nmaps)
  4848.             {
  4849.               key = new_click;
  4850.               break;
  4851.             }
  4852.               /* Otherwise, we'll leave key set to the drag event.  */
  4853.             }
  4854.         }
  4855.         }
  4856.     }
  4857.  
  4858.       keybuf[t++] = key;
  4859.       /* Normally, last_nonmenu_event gets the previous key we read.
  4860.      But when a mouse popup menu is being used,
  4861.      we don't update last_nonmenu_event; it continues to hold the mouse
  4862.      event that preceded the first level of menu.  */
  4863.       if (!used_mouse_menu)
  4864.     last_nonmenu_event = key;
  4865.  
  4866.       /* If the sequence is unbound, see if we can hang a function key
  4867.      off the end of it.  We only want to scan real keyboard input
  4868.      for function key sequences, so if mock_input says that we're
  4869.      re-reading old events, don't examine it.  */
  4870.       if ((first_binding >= nmaps || case_converted)
  4871.       && t >= mock_input)
  4872.     {
  4873.       Lisp_Object fkey_next;
  4874.  
  4875.       /* Continue scan from fkey_end until we find a bound suffix.
  4876.          If we fail, increment fkey_start
  4877.          and start fkey_end from there.  */
  4878.       while (fkey_end < t)
  4879.         {
  4880.           Lisp_Object key;
  4881.  
  4882.           key = keybuf[fkey_end++];
  4883.           /* Look up meta-characters by prefixing them
  4884.          with meta_prefix_char.  I hate this.  */
  4885.           if (XTYPE (key) == Lisp_Int && XINT (key) & meta_modifier)
  4886.         {
  4887.           fkey_next
  4888.             = get_keymap_1
  4889.               (get_keyelt
  4890.                (access_keymap (fkey_map, meta_prefix_char, 1, 0), 0),
  4891.                0, 1);
  4892.           XFASTINT (key) = XFASTINT (key) & ~meta_modifier;
  4893.         }
  4894.           else
  4895.         fkey_next = fkey_map;
  4896.  
  4897.           fkey_next
  4898.         = get_keyelt (access_keymap (fkey_next, key, 1, 0), 0);
  4899.  
  4900.           /* If the function key map gives a function, not an
  4901.          array, then call the function with no args and use
  4902.          its value instead.  */
  4903.           if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
  4904.           && fkey_end == t)
  4905.         {
  4906.           struct gcpro gcpro1, gcpro2, gcpro3;
  4907.           Lisp_Object tem;
  4908.           tem = fkey_next;
  4909.  
  4910.           GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
  4911.           fkey_next = call1 (fkey_next, prompt);
  4912.           UNGCPRO;
  4913.           /* If the function returned something invalid,
  4914.              barf--don't ignore it.
  4915.              (To ignore it safely, we would need to gcpro a bunch of 
  4916.              other variables.)  */
  4917.           if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
  4918.             error ("Function in function-key-map returns invalid key sequence");
  4919.         }
  4920.  
  4921.           function_key_possible = ! NILP (fkey_next);
  4922.  
  4923.           /* If keybuf[fkey_start..fkey_end] is bound in the
  4924.          function key map and it's a suffix of the current
  4925.          sequence (i.e. fkey_end == t), replace it with
  4926.          the binding and restart with fkey_start at the end. */
  4927.           if ((VECTORP (fkey_next) || STRINGP (fkey_next))
  4928.           && fkey_end == t)
  4929.         {
  4930.           int len = XFASTINT (Flength (fkey_next));
  4931.  
  4932.           t = fkey_start + len;
  4933.           if (t >= bufsize)
  4934.             error ("key sequence too long");
  4935.  
  4936.           if (VECTORP (fkey_next))
  4937.             bcopy (XVECTOR (fkey_next)->contents,
  4938.                keybuf + fkey_start,
  4939.                (t - fkey_start) * sizeof (keybuf[0]));
  4940.           else if (STRINGP (fkey_next))
  4941.             {
  4942.               int i;
  4943.  
  4944.               for (i = 0; i < len; i++)
  4945.             XFASTINT (keybuf[fkey_start + i])
  4946.               = XSTRING (fkey_next)->data[i];
  4947.             }
  4948.           
  4949.           mock_input = t;
  4950.           fkey_start = fkey_end = t;
  4951.           fkey_map = Vfunction_key_map;
  4952.  
  4953.           goto replay_sequence;
  4954.         }
  4955.           
  4956.           fkey_map = get_keymap_1 (fkey_next, 0, 1);
  4957.  
  4958.           /* If we no longer have a bound suffix, try a new positions for 
  4959.          fkey_start.  */
  4960.           if (NILP (fkey_map))
  4961.         {
  4962.           fkey_end = ++fkey_start;
  4963.           fkey_map = Vfunction_key_map;
  4964.           function_key_possible = 0;
  4965.         }
  4966.         }
  4967.     }
  4968.  
  4969.       /* Look for this sequence in key-translation-map.  */
  4970.       {
  4971.     Lisp_Object keytran_next;
  4972.  
  4973.     /* Scan from keytran_end until we find a bound suffix.  */
  4974.     while (keytran_end < t)
  4975.       {
  4976.         Lisp_Object key;
  4977.  
  4978.         key = keybuf[keytran_end++];
  4979.         /* Look up meta-characters by prefixing them
  4980.            with meta_prefix_char.  I hate this.  */
  4981.         if (XTYPE (key) == Lisp_Int && XINT (key) & meta_modifier)
  4982.           {
  4983.         keytran_next
  4984.           = get_keymap_1
  4985.             (get_keyelt
  4986.              (access_keymap (keytran_map, meta_prefix_char, 1, 0), 0),
  4987.              0, 1);
  4988.         XFASTINT (key) = XFASTINT (key) & ~meta_modifier;
  4989.           }
  4990.         else
  4991.           keytran_next = keytran_map;
  4992.  
  4993.         keytran_next
  4994.           = get_keyelt (access_keymap (keytran_next, key, 1, 0), 0);
  4995.  
  4996.         /* If the key translation map gives a function, not an
  4997.            array, then call the function with no args and use
  4998.            its value instead.  */
  4999.         if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
  5000.         && keytran_end == t)
  5001.           {
  5002.         struct gcpro gcpro1, gcpro2, gcpro3;
  5003.         Lisp_Object tem;
  5004.         tem = keytran_next;
  5005.  
  5006.         GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
  5007.         keytran_next = call1 (keytran_next, prompt);
  5008.         UNGCPRO;
  5009.         /* If the function returned something invalid,
  5010.            barf--don't ignore it.
  5011.            (To ignore it safely, we would need to gcpro a bunch of 
  5012.            other variables.)  */
  5013.         if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
  5014.           error ("Function in key-translation-map returns invalid key sequence");
  5015.           }
  5016.  
  5017.         /* If keybuf[keytran_start..keytran_end] is bound in the
  5018.            key translation map and it's a suffix of the current
  5019.            sequence (i.e. keytran_end == t), replace it with
  5020.            the binding and restart with keytran_start at the end. */
  5021.         if ((VECTORP (keytran_next) || STRINGP (keytran_next))
  5022.         && keytran_end == t)
  5023.           {
  5024.         int len = XFASTINT (Flength (keytran_next));
  5025.  
  5026.         t = keytran_start + len;
  5027.         if (t >= bufsize)
  5028.           error ("key sequence too long");
  5029.  
  5030.         if (VECTORP (keytran_next))
  5031.           bcopy (XVECTOR (keytran_next)->contents,
  5032.              keybuf + keytran_start,
  5033.              (t - keytran_start) * sizeof (keybuf[0]));
  5034.         else if (STRINGP (keytran_next))
  5035.           {
  5036.             int i;
  5037.  
  5038.             for (i = 0; i < len; i++)
  5039.               XFASTINT (keybuf[keytran_start + i])
  5040.             = XSTRING (keytran_next)->data[i];
  5041.           }
  5042.  
  5043.         mock_input = t;
  5044.         keytran_start = keytran_end = t;
  5045.         keytran_map = Vkey_translation_map;
  5046.  
  5047.         goto replay_sequence;
  5048.           }
  5049.  
  5050.         keytran_map = get_keymap_1 (keytran_next, 0, 1);
  5051.  
  5052.         /* If we no longer have a bound suffix, try a new positions for 
  5053.            keytran_start.  */
  5054.         if (NILP (keytran_map))
  5055.           {
  5056.         keytran_end = ++keytran_start;
  5057.         keytran_map = Vkey_translation_map;
  5058.           }
  5059.       }
  5060.       }
  5061.     }
  5062.  
  5063.   read_key_sequence_cmd = (first_binding < nmaps
  5064.                ? defs[first_binding]
  5065.                : Qnil);
  5066.  
  5067.  done:
  5068.   unread_switch_frame = delayed_switch_frame;
  5069.   unbind_to (count, Qnil);
  5070.  
  5071.   /* Occasionally we fabricate events, perhaps by expanding something
  5072.      according to function-key-map, or by adding a prefix symbol to a
  5073.      mouse click in the scroll bar or modeline.  In this cases, return
  5074.      the entire generated key sequence, even if we hit an unbound
  5075.      prefix or a definition before the end.  This means that you will
  5076.      be able to push back the event properly, and also means that
  5077.      read-key-sequence will always return a logical unit.
  5078.  
  5079.      Better ideas?  */
  5080.   for (; t < mock_input; t++)
  5081.     {
  5082.       echo_char (keybuf[t]);
  5083.       add_command_key (keybuf[t]);
  5084.     }
  5085.  
  5086.   return t;
  5087. }
  5088.  
  5089. #if 0 /* This doc string is too long for some compilers.
  5090.      This commented-out definition serves for DOC.  */
  5091. DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
  5092.   "Read a sequence of keystrokes and return as a string or vector.\n\
  5093. The sequence is sufficient to specify a non-prefix command in the\n\
  5094. current local and global maps.\n\
  5095. \n\
  5096. First arg PROMPT is a prompt string.  If nil, do not prompt specially.\n\
  5097. Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
  5098. as a continuation of the previous key.\n\
  5099. \n\
  5100. A C-g typed while in this function is treated like any other character,\n\
  5101. and `quit-flag' is not set.\n\
  5102. \n\
  5103. If the key sequence starts with a mouse click, then the sequence is read\n\
  5104. using the keymaps of the buffer of the window clicked in, not the buffer\n\
  5105. of the selected window as normal.\n\
  5106. ""\n\
  5107. `read-key-sequence' drops unbound button-down events, since you normally\n\
  5108. only care about the click or drag events which follow them.  If a drag\n\
  5109. or multi-click event is unbound, but the corresponding click event would\n\
  5110. be bound, `read-key-sequence' turns the event into a click event at the\n\
  5111. drag's starting position.  This means that you don't have to distinguish\n\
  5112. between click and drag, double, or triple events unless you want to.\n\
  5113. \n\
  5114. `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
  5115. lines separating windows, and scroll bars with imaginary keys\n\
  5116. `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
  5117. \n\
  5118. If the user switches frames in the middle of a key sequence, the\n\
  5119. frame-switch event is put off until after the current key sequence.\n\
  5120. \n\
  5121. `read-key-sequence' checks `function-key-map' for function key\n\
  5122. sequences, where they wouldn't conflict with ordinary bindings.  See\n\
  5123. `function-key-map' for more details.")
  5124.   (prompt, continue_echo)
  5125. #endif
  5126.  
  5127. DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
  5128.   0)
  5129.   (prompt, continue_echo)
  5130.      Lisp_Object prompt, continue_echo;
  5131. {
  5132.   Lisp_Object keybuf[30];
  5133.   register int i;
  5134.   struct gcpro gcpro1, gcpro2;
  5135.  
  5136.   if (!NILP (prompt))
  5137.     CHECK_STRING (prompt, 0);
  5138.   QUIT;
  5139.  
  5140.   bzero (keybuf, sizeof keybuf);
  5141.   GCPRO1 (keybuf[0]);
  5142.   gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
  5143.  
  5144.   if (NILP (continue_echo))
  5145.     this_command_key_count = 0;
  5146.  
  5147.   i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])), prompt);
  5148.  
  5149.   if (i == -1)
  5150.     {
  5151.       Vquit_flag = Qt;
  5152.       QUIT;
  5153.     }
  5154.   UNGCPRO;
  5155.   return make_event_array (i, keybuf);
  5156. }
  5157.  
  5158. DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
  5159.  "Execute CMD as an editor command.\n\
  5160. CMD must be a symbol that satisfies the `commandp' predicate.\n\
  5161. Optional second arg RECORD-FLAG non-nil\n\
  5162. means unconditionally put this command in `command-history'.\n\
  5163. Otherwise, that is done only if an arg is read using the minibuffer.")
  5164.      (cmd, record)
  5165.      Lisp_Object cmd, record;
  5166. {
  5167.   register Lisp_Object final;
  5168.   register Lisp_Object tem;
  5169.   Lisp_Object prefixarg;
  5170.   struct backtrace backtrace;
  5171.   extern int debug_on_next_call;
  5172.  
  5173.   prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
  5174.   Vcurrent_prefix_arg = prefixarg;
  5175.   debug_on_next_call = 0;
  5176.  
  5177.   if (XTYPE (cmd) == Lisp_Symbol)
  5178.     {
  5179.       tem = Fget (cmd, Qdisabled);
  5180.       if (!NILP (tem) && !NILP (Vrun_hooks))
  5181.     return call1 (Vrun_hooks, Qdisabled_command_hook);
  5182.     }
  5183.  
  5184.   while (1)
  5185.     {
  5186.       final = Findirect_function (cmd);
  5187.  
  5188.       if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
  5189.     do_autoload (final, cmd);
  5190.       else
  5191.     break;
  5192.     }
  5193.  
  5194.   if (XTYPE (final) == Lisp_String
  5195.       || XTYPE (final) == Lisp_Vector)
  5196.     {
  5197.       /* If requested, place the macro in the command history.  For
  5198.      other sorts of commands, call-interactively takes care of
  5199.      this.  */
  5200.       if (!NILP (record))
  5201.     Vcommand_history
  5202.       = Fcons (Fcons (Qexecute_kbd_macro,
  5203.               Fcons (final, Fcons (prefixarg, Qnil))),
  5204.            Vcommand_history);
  5205.  
  5206.       return Fexecute_kbd_macro (final, prefixarg);
  5207.     }
  5208.   if (CONSP (final) || XTYPE (final) == Lisp_Subr
  5209.       || XTYPE (final) == Lisp_Compiled)
  5210.     {
  5211.       backtrace.next = backtrace_list;
  5212.       backtrace_list = &backtrace;
  5213.       backtrace.function = &Qcall_interactively;
  5214.       backtrace.args = &cmd;
  5215.       backtrace.nargs = 1;
  5216.       backtrace.evalargs = 0;
  5217.  
  5218.       tem = Fcall_interactively (cmd, record);
  5219.  
  5220.       backtrace_list = backtrace.next;
  5221.       return tem;
  5222.     }
  5223.   return Qnil;
  5224. }
  5225.  
  5226. DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
  5227.   1, 1, "P",
  5228.   "Read function name, then read its arguments and call it.")
  5229.   (prefixarg)
  5230.      Lisp_Object prefixarg;
  5231. {
  5232.   Lisp_Object function;
  5233.   char buf[40];
  5234.   Lisp_Object saved_keys;
  5235.   struct gcpro gcpro1;
  5236.  
  5237.   saved_keys = Fvector (this_command_key_count,
  5238.             XVECTOR (this_command_keys)->contents);
  5239.   buf[0] = 0;
  5240.   GCPRO1 (saved_keys);
  5241.  
  5242.   if (EQ (prefixarg, Qminus))
  5243.     strcpy (buf, "- ");
  5244.   else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
  5245.     strcpy (buf, "C-u ");
  5246.   else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
  5247.     sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
  5248.   else if (XTYPE (prefixarg) == Lisp_Int)
  5249.     sprintf (buf, "%d ", XINT (prefixarg));
  5250.  
  5251.   /* This isn't strictly correct if execute-extended-command
  5252.      is bound to anything else.  Perhaps it should use
  5253.      this_command_keys?  */
  5254.   strcat (buf, "M-x ");
  5255.  
  5256.   /* Prompt with buf, and then read a string, completing from and
  5257.      restricting to the set of all defined commands.  Don't provide
  5258.      any initial input.  Save the command read on the extended-command
  5259.      history list. */
  5260.   function = Fcompleting_read (build_string (buf),
  5261.                    Vobarray, Qcommandp,
  5262.                    Qt, Qnil, Qextended_command_history);
  5263.  
  5264.   /* Set this_command_keys to the concatenation of saved_keys and
  5265.      function, followed by a RET.  */
  5266.   {
  5267.     struct Lisp_String *str;
  5268.     Lisp_Object *keys;
  5269.     int i;
  5270.     Lisp_Object tem;
  5271.  
  5272.     this_command_key_count = 0;
  5273.  
  5274.     keys = XVECTOR (saved_keys)->contents;
  5275.     for (i = 0; i < XVECTOR (saved_keys)->size; i++)
  5276.       add_command_key (keys[i]);
  5277.  
  5278.     str = XSTRING (function);
  5279.     for (i = 0; i < str->size; i++)
  5280.       {
  5281.     XFASTINT (tem) = str->data[i];
  5282.     add_command_key (tem);
  5283.       }
  5284.  
  5285.     XFASTINT (tem) = '\015';
  5286.     add_command_key (tem);
  5287.   }
  5288.  
  5289.   UNGCPRO;
  5290.  
  5291.   function = Fintern (function, Qnil);
  5292.   Vprefix_arg = prefixarg;
  5293.   this_command = function;
  5294.  
  5295.   return Fcommand_execute (function, Qt);
  5296. }
  5297.  
  5298.  
  5299. detect_input_pending ()
  5300. {
  5301.   if (!input_pending)
  5302.     get_input_pending (&input_pending);
  5303.  
  5304.   return input_pending;
  5305. }
  5306.  
  5307. /* This is called in some cases before a possible quit.
  5308.    It cases the next call to detect_input_pending to recompute input_pending.
  5309.    So calling this function unnecessarily can't do any harm.  */
  5310. clear_input_pending ()
  5311. {
  5312.   input_pending = 0;
  5313. }
  5314.  
  5315. DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
  5316.   "T if command input is currently available with no waiting.\n\
  5317. Actually, the value is nil only if we can be sure that no input is available.")
  5318.   ()
  5319. {
  5320.   if (!NILP (Vunread_command_events) || unread_command_char != -1)
  5321.     return (Qt);
  5322.  
  5323.   return detect_input_pending () ? Qt : Qnil;
  5324. }
  5325.  
  5326. DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
  5327.   "Return vector of last 100 events, not counting those from keyboard macros.")
  5328.   ()
  5329. {
  5330.   Lisp_Object *keys = XVECTOR (recent_keys)->contents;
  5331.   Lisp_Object val;
  5332.  
  5333.   if (total_keys < NUM_RECENT_KEYS)
  5334.     return Fvector (total_keys, keys);
  5335.   else
  5336.     {
  5337.       val = Fvector (NUM_RECENT_KEYS, keys);
  5338.       bcopy (keys + recent_keys_index,
  5339.          XVECTOR (val)->contents,
  5340.          (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
  5341.       bcopy (keys,
  5342.          XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
  5343.          recent_keys_index * sizeof (Lisp_Object));
  5344.       return val;
  5345.     }
  5346. }
  5347.  
  5348. DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
  5349.   "Return the key sequence that invoked this command.\n\
  5350. The value is a string or a vector.")
  5351.   ()
  5352. {
  5353.   return make_event_array (this_command_key_count,
  5354.                XVECTOR (this_command_keys)->contents);
  5355. }
  5356.  
  5357. DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
  5358.   "Return the current depth in recursive edits.")
  5359.   ()
  5360. {
  5361.   Lisp_Object temp;
  5362.   XFASTINT (temp) = command_loop_level + minibuf_level;
  5363.   return temp;
  5364. }
  5365.  
  5366. DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
  5367.   "FOpen dribble file: ",
  5368.   "Start writing all keyboard characters to a dribble file called FILE.\n\
  5369. If FILE is nil, close any open dribble file.")
  5370.   (file)
  5371.      Lisp_Object file;
  5372. {
  5373.   if (NILP (file))
  5374.     {
  5375.       fclose (dribble);
  5376.       dribble = 0;
  5377.     }
  5378.   else
  5379.     {
  5380.       file = Fexpand_file_name (file, Qnil);
  5381.       dribble = fopen (XSTRING (file)->data, "w");
  5382.     }
  5383.   return Qnil;
  5384. }
  5385.  
  5386. DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
  5387.   "Discard the contents of the terminal input buffer.\n\
  5388. Also cancel any kbd macro being defined.")
  5389.   ()
  5390. {
  5391.   defining_kbd_macro = 0;
  5392.   update_mode_lines++;
  5393.  
  5394.   Vunread_command_events = Qnil;
  5395.   unread_command_char = -1;
  5396.  
  5397.   discard_tty_input ();
  5398.  
  5399.   /* Without the cast, GCC complains that this assignment loses the
  5400.      volatile qualifier of kbd_store_ptr.  Is there anything wrong
  5401.      with that?  */
  5402.   kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
  5403.   Ffillarray (kbd_buffer_frame_or_window, Qnil);
  5404.   input_pending = 0;
  5405.  
  5406.   return Qnil;
  5407. }
  5408.  
  5409. DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
  5410.   "Stop Emacs and return to superior process.  You can resume later.\n\
  5411. If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
  5412. control, run a subshell instead.\n\n\
  5413. If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
  5414. to be read as terminal input by Emacs's parent, after suspension.\n\
  5415. \n\
  5416. Before suspending, call the functions in `suspend-hook' with no args.\n\
  5417. If any of them returns nil, don't call the rest and don't suspend.\n\
  5418. Otherwise, suspend normally and after resumption run the normal hook\n\
  5419. `suspend-resume-hook' if that is bound and non-nil.\n\
  5420. \n\
  5421. Some operating systems cannot stop the Emacs process and resume it later.\n\
  5422. On such systems, Emacs starts a subshell instead of suspending.")
  5423.   (stuffstring)
  5424.      Lisp_Object stuffstring;
  5425. {
  5426.   Lisp_Object tem;
  5427.   int count = specpdl_ptr - specpdl;
  5428.   int old_height, old_width;
  5429.   int width, height;
  5430.   struct gcpro gcpro1, gcpro2;
  5431.   extern init_sys_modes ();
  5432.  
  5433.   if (!NILP (stuffstring))
  5434.     CHECK_STRING (stuffstring, 0);
  5435.  
  5436.   /* Run the functions in suspend-hook.  */
  5437.   if (!NILP (Vrun_hooks))
  5438.     call1 (Vrun_hooks, intern ("suspend-hook"));
  5439.  
  5440.   GCPRO1 (stuffstring);
  5441.   get_frame_size (&old_width, &old_height);
  5442.   reset_sys_modes ();
  5443.   /* sys_suspend can get an error if it tries to fork a subshell
  5444.      and the system resources aren't available for that.  */
  5445.   record_unwind_protect (init_sys_modes, 0);
  5446.   stuff_buffered_input (stuffstring);
  5447.   if (cannot_suspend)
  5448.     sys_subshell ();
  5449.   else
  5450.     sys_suspend ();
  5451.   unbind_to (count, Qnil);
  5452.  
  5453.   /* Check if terminal/window size has changed.
  5454.      Note that this is not useful when we are running directly
  5455.      with a window system; but suspend should be disabled in that case.  */
  5456.   get_frame_size (&width, &height);
  5457.   if (width != old_width || height != old_height)
  5458.     change_frame_size (selected_frame, height, width, 0, 0);
  5459.  
  5460.   /* Run suspend-resume-hook.  */
  5461.   if (!NILP (Vrun_hooks))
  5462.     call1 (Vrun_hooks, intern ("suspend-resume-hook"));
  5463.   
  5464.   UNGCPRO;
  5465.   return Qnil;
  5466. }
  5467.  
  5468. /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
  5469.    Then in any case stuff anything Emacs has read ahead and not used.  */
  5470.  
  5471. stuff_buffered_input (stuffstring)
  5472.      Lisp_Object stuffstring;
  5473. {
  5474.   register unsigned char *p;
  5475.  
  5476. /* stuff_char works only in BSD, versions 4.2 and up.  */
  5477. #ifdef BSD
  5478. #ifndef BSD4_1
  5479.   if (XTYPE (stuffstring) == Lisp_String)
  5480.     {
  5481.       register int count;
  5482.  
  5483.       p = XSTRING (stuffstring)->data;
  5484.       count = XSTRING (stuffstring)->size;
  5485.       while (count-- > 0)
  5486.     stuff_char (*p++);
  5487.       stuff_char ('\n');
  5488.     }
  5489.   /* Anything we have read ahead, put back for the shell to read.  */
  5490.   while (kbd_fetch_ptr != kbd_store_ptr)
  5491.     {
  5492.       if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
  5493.     kbd_fetch_ptr = kbd_buffer;
  5494.       if (kbd_fetch_ptr->kind == ascii_keystroke)
  5495.     stuff_char (kbd_fetch_ptr->code);
  5496.       kbd_fetch_ptr->kind = no_event;
  5497.       (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
  5498.                              - kbd_buffer]
  5499.        = Qnil);
  5500.       kbd_fetch_ptr++;
  5501.     }
  5502.   input_pending = 0;
  5503. #endif
  5504. #endif /* BSD and not BSD4_1 */
  5505. }
  5506.  
  5507. set_waiting_for_input (time_to_clear)
  5508.      EMACS_TIME *time_to_clear;
  5509. {
  5510.   input_available_clear_time = time_to_clear;
  5511.  
  5512.   /* Tell interrupt_signal to throw back to read_char,  */
  5513.   waiting_for_input = 1;
  5514.  
  5515.   /* If interrupt_signal was called before and buffered a C-g,
  5516.      make it run again now, to avoid timing error. */
  5517.   if (!NILP (Vquit_flag))
  5518.     quit_throw_to_read_char ();
  5519. }
  5520.  
  5521. clear_waiting_for_input ()
  5522. {
  5523.   /* Tell interrupt_signal not to throw back to read_char,  */
  5524.   waiting_for_input = 0;
  5525.   input_available_clear_time = 0;
  5526. }
  5527.  
  5528. /* This routine is called at interrupt level in response to C-G.
  5529.  If interrupt_input, this is the handler for SIGINT.
  5530.  Otherwise, it is called from kbd_buffer_store_event,
  5531.  in handling SIGIO or SIGTINT.
  5532.  
  5533.  If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
  5534.  immediately throw back to read_char.
  5535.  
  5536.  Otherwise it sets the Lisp variable  quit-flag  not-nil.
  5537.  This causes  eval  to throw, when it gets a chance.
  5538.  If  quit-flag  is already non-nil, it stops the job right away.  */
  5539.  
  5540. SIGTYPE
  5541. interrupt_signal ()
  5542. {
  5543.   char c;
  5544.   /* Must preserve main program's value of errno.  */
  5545.   int old_errno = errno;
  5546.  
  5547. #ifdef USG
  5548.   /* USG systems forget handlers when they are used;
  5549.      must reestablish each time */
  5550.   signal (SIGINT, interrupt_signal);
  5551.   signal (SIGQUIT, interrupt_signal);
  5552. #endif /* USG */
  5553.  
  5554.   cancel_echoing ();
  5555.  
  5556.   if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
  5557.     {
  5558.       fflush (stdout);
  5559.       reset_sys_modes ();
  5560.       sigfree ();
  5561. #ifdef SIGTSTP            /* Support possible in later USG versions */
  5562. /*
  5563.  * On systems which can suspend the current process and return to the original
  5564.  * shell, this command causes the user to end up back at the shell.
  5565.  * The "Auto-save" and "Abort" questions are not asked until
  5566.  * the user elects to return to emacs, at which point he can save the current
  5567.  * job and either dump core or continue.
  5568.  */
  5569.       sys_suspend ();
  5570. #else
  5571. #ifdef VMS
  5572.       if (sys_suspend () == -1)
  5573.     {
  5574.       printf ("Not running as a subprocess;\n");
  5575.       printf ("you can continue or abort.\n");
  5576.     }
  5577. #else /* not VMS */
  5578.       /* Perhaps should really fork an inferior shell?
  5579.      But that would not provide any way to get back
  5580.      to the original shell, ever.  */
  5581.       printf ("No support for stopping a process on this operating system;\n");
  5582.       printf ("you can continue or abort.\n");
  5583. #endif /* not VMS */
  5584. #endif /* not SIGTSTP */
  5585. #ifdef MSDOS
  5586.       /* We must remain inside the screen area when the internal terminal
  5587.      is used.  Note that [Enter] is not echoed by dos.  */
  5588.       cursor_to (0, 0);
  5589. #endif
  5590.       printf ("Auto-save? (y or n) ");
  5591.       fflush (stdout);
  5592.       if (((c = getchar ()) & ~040) == 'Y')
  5593.     {
  5594.       Fdo_auto_save (Qt, Qnil);
  5595. #ifdef MSDOS
  5596.       printf ("\r\nAuto-save done");
  5597. #else /* not MSDOS */
  5598.       printf ("Auto-save done\n");
  5599. #endif /* not MSDOS */
  5600.     }
  5601.       while (c != '\n') c = getchar ();
  5602. #ifdef MSDOS
  5603.       printf ("\r\nAbort?  (y or n) ");
  5604. #else /* not MSDOS */
  5605. #ifdef VMS
  5606.       printf ("Abort (and enter debugger)? (y or n) ");
  5607. #else /* not VMS */
  5608.       printf ("Abort (and dump core)? (y or n) ");
  5609. #endif /* not VMS */
  5610. #endif /* not MSDOS */
  5611.       fflush (stdout);
  5612.       if (((c = getchar ()) & ~040) == 'Y')
  5613.     abort ();
  5614.       while (c != '\n') c = getchar ();
  5615. #ifdef MSDOS
  5616.       printf ("\r\nContinuing...\r\n");
  5617. #else /* not MSDOS */
  5618.       printf ("Continuing...\n");
  5619. #endif /* not MSDOS */
  5620.       fflush (stdout);
  5621.       init_sys_modes ();
  5622.     }
  5623.   else
  5624.     {
  5625.       /* If executing a function that wants to be interrupted out of
  5626.      and the user has not deferred quitting by binding `inhibit-quit'
  5627.      then quit right away.  */
  5628.       if (immediate_quit && NILP (Vinhibit_quit))
  5629.     {
  5630.       immediate_quit = 0;
  5631.           sigfree ();
  5632.       Fsignal (Qquit, Qnil);
  5633.     }
  5634.       else
  5635.     /* Else request quit when it's safe */
  5636.     Vquit_flag = Qt;
  5637.     }
  5638.  
  5639.   if (waiting_for_input && !echoing)
  5640.     quit_throw_to_read_char ();
  5641.  
  5642.   errno = old_errno;
  5643. }
  5644.  
  5645. /* Handle a C-g by making read_char return C-g.  */
  5646.  
  5647. quit_throw_to_read_char ()
  5648. {
  5649.   quit_error_check ();
  5650.   sigfree ();
  5651.   /* Prevent another signal from doing this before we finish.  */
  5652.   clear_waiting_for_input ();
  5653.   input_pending = 0;
  5654.  
  5655.   Vunread_command_events = Qnil;
  5656.   unread_command_char = -1;
  5657.  
  5658. #ifdef POLL_FOR_INPUT
  5659.   /* May be > 1 if in recursive minibuffer.  */
  5660.   if (poll_suppress_count == 0)
  5661.     abort ();
  5662. #endif
  5663. #ifdef MULTI_FRAME
  5664.   if (XTYPE (internal_last_event_frame) == Lisp_Frame
  5665.       && XFRAME (internal_last_event_frame) != selected_frame)
  5666.     Fhandle_switch_frame (make_lispy_switch_frame (internal_last_event_frame));
  5667. #endif
  5668.  
  5669.   _longjmp (getcjmp, 1);
  5670. }
  5671.  
  5672. DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
  5673.   "Set mode of reading keyboard input.\n\
  5674. First arg INTERRUPT non-nil means use input interrupts;\n\
  5675.  nil means use CBREAK mode.\n\
  5676. Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
  5677.  (no effect except in CBREAK mode).\n\
  5678. Third arg META t means accept 8-bit input (for a Meta key).\n\
  5679.  META nil means ignore the top bit, on the assumption it is parity.\n\
  5680.  Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
  5681. Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
  5682. See also `current-input-mode'.")
  5683.   (interrupt, flow, meta, quit)
  5684.      Lisp_Object interrupt, flow, meta, quit;
  5685. {
  5686.   if (!NILP (quit)
  5687.       && (XTYPE (quit) != Lisp_Int
  5688.       || XINT (quit) < 0 || XINT (quit) > 0400))
  5689.     error ("set-input-mode: QUIT must be an ASCII character");
  5690.  
  5691. #ifdef POLL_FOR_INPUT
  5692.   stop_polling ();
  5693. #endif
  5694.  
  5695.   reset_sys_modes ();
  5696. #ifdef SIGIO
  5697. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  5698. #ifdef NO_SOCK_SIGIO
  5699.   if (read_socket_hook)
  5700.     interrupt_input = 0;    /* No interrupts if reading from a socket.  */
  5701.   else
  5702. #endif /* NO_SOCK_SIGIO */
  5703.     interrupt_input = !NILP (interrupt);
  5704. #else /* not SIGIO */
  5705.   interrupt_input = 0;
  5706. #endif /* not SIGIO */
  5707. /* Our VMS input only works by interrupts, as of now.  */
  5708. #ifdef VMS
  5709.   interrupt_input = 1;
  5710. #endif
  5711.   flow_control = !NILP (flow);
  5712.   if (NILP (meta))
  5713.     meta_key = 0;
  5714.   else if (EQ (meta, Qt))
  5715.     meta_key = 1;
  5716.   else
  5717.     meta_key = 2;
  5718.   if (!NILP (quit))
  5719.     /* Don't let this value be out of range.  */
  5720.     quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
  5721.  
  5722.   init_sys_modes ();
  5723.  
  5724. #ifdef POLL_FOR_INPUT
  5725.   poll_suppress_count = 1;
  5726.   start_polling ();
  5727. #endif
  5728.   return Qnil;
  5729. }
  5730.  
  5731. DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
  5732.   "Return information about the way Emacs currently reads keyboard input.\n\
  5733. The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
  5734.   INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
  5735.     nil, Emacs is using CBREAK mode.\n\
  5736.   FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
  5737.     terminal; this does not apply if Emacs uses interrupt-driven input.\n\
  5738.   META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
  5739.     META nil means ignoring the top bit, on the assumption it is parity.\n\
  5740.     META is neither t nor nil if accepting 8-bit input and using\n\
  5741.     all 8 bits as the character code.\n\
  5742.   QUIT is the character Emacs currently uses to quit.\n\
  5743. The elements of this list correspond to the arguments of\n\
  5744. `set-input-mode'.")
  5745.   ()
  5746. {
  5747.   Lisp_Object val[4];
  5748.  
  5749.   val[0] = interrupt_input ? Qt : Qnil;
  5750.   val[1] = flow_control ? Qt : Qnil;
  5751.   val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
  5752.   XFASTINT (val[3]) = quit_char;
  5753.  
  5754.   return Flist (sizeof (val) / sizeof (val[0]), val);
  5755. }
  5756.  
  5757.  
  5758. init_keyboard ()
  5759. {
  5760.   /* This is correct before outermost invocation of the editor loop */
  5761.   command_loop_level = -1;
  5762.   immediate_quit = 0;
  5763.   quit_char = Ctl ('g');
  5764.   Vunread_command_events = Qnil;
  5765.   unread_command_char = -1;
  5766.   total_keys = 0;
  5767.   recent_keys_index = 0;
  5768.   kbd_fetch_ptr = kbd_buffer;
  5769.   kbd_store_ptr = kbd_buffer;
  5770.   do_mouse_tracking = 0;
  5771.   input_pending = 0;
  5772.  
  5773. #ifdef MULTI_FRAME
  5774.   /* This means that command_loop_1 won't try to select anything the first
  5775.      time through.  */
  5776.   internal_last_event_frame = Qnil;
  5777.   Vlast_event_frame = internal_last_event_frame;
  5778. #endif
  5779.  
  5780.   /* If we're running a dumped Emacs, we need to clear out
  5781.      kbd_buffer_frame_or_window, in case some events got into it
  5782.      before we dumped.
  5783.  
  5784.      If we're running an undumped Emacs, it hasn't been initialized by
  5785.      syms_of_keyboard yet.  */
  5786.   if (initialized)
  5787.     Ffillarray (kbd_buffer_frame_or_window, Qnil);
  5788.  
  5789.   if (!noninteractive)
  5790.     {
  5791.       signal (SIGINT, interrupt_signal);
  5792. #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
  5793.       /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
  5794.      SIGQUIT and we can't tell which one it will give us.  */
  5795.       signal (SIGQUIT, interrupt_signal);
  5796. #endif /* HAVE_TERMIO */
  5797. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  5798. #ifdef SIGIO
  5799.       signal (SIGIO, input_available_signal);
  5800. #endif /* SIGIO */
  5801.     }
  5802.  
  5803. /* Use interrupt input by default, if it works and noninterrupt input
  5804.    has deficiencies.  */
  5805.  
  5806. #ifdef INTERRUPT_INPUT
  5807.   interrupt_input = 1;
  5808. #else
  5809.   interrupt_input = 0;
  5810. #endif
  5811.  
  5812. /* Our VMS input only works by interrupts, as of now.  */
  5813. #ifdef VMS
  5814.   interrupt_input = 1;
  5815. #endif
  5816.  
  5817.   sigfree ();
  5818.   dribble = 0;
  5819.  
  5820.   if (keyboard_init_hook)
  5821.     (*keyboard_init_hook) ();
  5822.  
  5823. #ifdef POLL_FOR_INPUT
  5824.   poll_suppress_count = 1;
  5825.   start_polling ();
  5826. #endif
  5827. }
  5828.  
  5829. /* This type's only use is in syms_of_keyboard, to initialize the 
  5830.    event header symbols and put properties on them.  */
  5831. struct event_head {
  5832.   Lisp_Object *var;
  5833.   char *name;
  5834.   Lisp_Object *kind;
  5835. };
  5836.  
  5837. struct event_head head_table[] = {
  5838.   &Qmouse_movement,    "mouse-movement",    &Qmouse_movement,
  5839.   &Qscroll_bar_movement, "scroll-bar-movement",    &Qmouse_movement,
  5840.   &Qswitch_frame,    "switch-frame",        &Qswitch_frame,
  5841. };
  5842.  
  5843. syms_of_keyboard ()
  5844. {
  5845.   Qdisabled_command_hook = intern ("disabled-command-hook");
  5846.   staticpro (&Qdisabled_command_hook);
  5847.  
  5848.   Qself_insert_command = intern ("self-insert-command");
  5849.   staticpro (&Qself_insert_command);
  5850.  
  5851.   Qforward_char = intern ("forward-char");
  5852.   staticpro (&Qforward_char);
  5853.  
  5854.   Qbackward_char = intern ("backward-char");
  5855.   staticpro (&Qbackward_char);
  5856.  
  5857.   Qdisabled = intern ("disabled");
  5858.   staticpro (&Qdisabled);
  5859.  
  5860.   Qundefined = intern ("undefined");
  5861.   staticpro (&Qundefined);
  5862.  
  5863.   Qpre_command_hook = intern ("pre-command-hook");
  5864.   staticpro (&Qpre_command_hook);
  5865.  
  5866.   Qpost_command_hook = intern ("post-command-hook");
  5867.   staticpro (&Qpost_command_hook);
  5868.  
  5869.   Qcommand_hook_internal = intern ("command-hook-internal");
  5870.   staticpro (&Qcommand_hook_internal);
  5871.  
  5872.   Qfunction_key = intern ("function-key");
  5873.   staticpro (&Qfunction_key);
  5874.   Qmouse_click = intern ("mouse-click");
  5875.   staticpro (&Qmouse_click);
  5876.  
  5877.   Qmenu_enable = intern ("menu-enable");
  5878.   staticpro (&Qmenu_enable);
  5879.  
  5880.   Qmode_line = intern ("mode-line");
  5881.   staticpro (&Qmode_line);
  5882.   Qvertical_line = intern ("vertical-line");
  5883.   staticpro (&Qvertical_line);
  5884.   Qvertical_scroll_bar = intern ("vertical-scroll-bar");
  5885.   staticpro (&Qvertical_scroll_bar);
  5886.   Qmenu_bar = intern ("menu-bar");
  5887.   staticpro (&Qmenu_bar);
  5888.  
  5889.   Qabove_handle = intern ("above-handle");
  5890.   staticpro (&Qabove_handle);
  5891.   Qhandle = intern ("handle");
  5892.   staticpro (&Qhandle);
  5893.   Qbelow_handle = intern ("below-handle");
  5894.   staticpro (&Qbelow_handle);
  5895.  
  5896.   Qevent_kind = intern ("event-kind");
  5897.   staticpro (&Qevent_kind);
  5898.   Qevent_symbol_elements = intern ("event-symbol-elements");
  5899.   staticpro (&Qevent_symbol_elements);
  5900.   Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
  5901.   staticpro (&Qevent_symbol_element_mask);
  5902.   Qmodifier_cache = intern ("modifier-cache");
  5903.   staticpro (&Qmodifier_cache);
  5904.  
  5905.   Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
  5906.   staticpro (&Qrecompute_lucid_menubar);
  5907.   Qactivate_menubar_hook = intern ("activate-menubar-hook");
  5908.   staticpro (&Qactivate_menubar_hook);
  5909.  
  5910.   Qpolling_period = intern ("polling-period");
  5911.   staticpro (&Qpolling_period);
  5912.  
  5913.   {
  5914.     struct event_head *p;
  5915.  
  5916.     for (p = head_table;
  5917.      p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
  5918.      p++)
  5919.       {
  5920.     *p->var = intern (p->name);
  5921.     staticpro (p->var);
  5922.     Fput (*p->var, Qevent_kind, *p->kind);
  5923.     Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
  5924.       }
  5925.   }
  5926.  
  5927.   button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
  5928.   staticpro (&button_down_location);
  5929.  
  5930.   {
  5931.     int i;
  5932.     int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
  5933.  
  5934.     modifier_symbols = Fmake_vector (make_number (len), Qnil);
  5935.     for (i = 0; i < len; i++)
  5936.       if (modifier_names[i])
  5937.     XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
  5938.     staticpro (&modifier_symbols);
  5939.   }
  5940.  
  5941.   recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
  5942.   staticpro (&recent_keys);
  5943.  
  5944.   this_command_keys = Fmake_vector (make_number (40), Qnil);
  5945.   staticpro (&this_command_keys);
  5946.  
  5947.   Qextended_command_history = intern ("extended-command-history");
  5948.   Fset (Qextended_command_history, Qnil);
  5949.   staticpro (&Qextended_command_history);
  5950.  
  5951.   kbd_buffer_frame_or_window
  5952.     = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
  5953.   staticpro (&kbd_buffer_frame_or_window);
  5954.  
  5955.   accent_key_syms = Qnil;
  5956.   staticpro (&accent_key_syms);
  5957.  
  5958.   func_key_syms = Qnil;
  5959.   staticpro (&func_key_syms);
  5960.  
  5961.   system_key_syms = Qnil;
  5962.   staticpro (&system_key_syms);
  5963.  
  5964.   mouse_syms = Qnil;
  5965.   staticpro (&mouse_syms);
  5966.  
  5967.   unread_switch_frame = Qnil;
  5968.   staticpro (&unread_switch_frame);
  5969.  
  5970.   defsubr (&Sread_key_sequence);
  5971.   defsubr (&Srecursive_edit);
  5972.   defsubr (&Strack_mouse);
  5973.   defsubr (&Sinput_pending_p);
  5974.   defsubr (&Scommand_execute);
  5975.   defsubr (&Srecent_keys);
  5976.   defsubr (&Sthis_command_keys);
  5977.   defsubr (&Ssuspend_emacs);
  5978.   defsubr (&Sabort_recursive_edit);
  5979.   defsubr (&Sexit_recursive_edit);
  5980.   defsubr (&Srecursion_depth);
  5981.   defsubr (&Stop_level);
  5982.   defsubr (&Sdiscard_input);
  5983.   defsubr (&Sopen_dribble_file);
  5984.   defsubr (&Sset_input_mode);
  5985.   defsubr (&Scurrent_input_mode);
  5986.   defsubr (&Sexecute_extended_command);
  5987.  
  5988.   DEFVAR_LISP ("last-command-char", &last_command_char,
  5989.     "Last input event that was part of a command.");
  5990.  
  5991.   DEFVAR_LISP ("last-command-event", &last_command_char,
  5992.     "Last input event that was part of a command.");
  5993.  
  5994.   DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
  5995.     "Last input event in a command, except for mouse menu events.\n\
  5996. Mouse menus give back keys that don't look like mouse events;\n\
  5997. this variable holds the actual mouse event that led to the menu,\n\
  5998. so that you can determine whether the command was run by mouse or not.");
  5999.  
  6000.   DEFVAR_LISP ("last-input-char", &last_input_char,
  6001.     "Last input event.");
  6002.  
  6003.   DEFVAR_LISP ("last-input-event", &last_input_char,
  6004.     "Last input event.");
  6005.  
  6006.   DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
  6007.     "List of objects to be read as next command input events.");
  6008.  
  6009.   DEFVAR_INT ("unread-command-char", &unread_command_char,
  6010.     "If not -1, an object to be read as next command input event.");
  6011.  
  6012.   DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
  6013.     "Meta-prefix character code.  Meta-foo as command input\n\
  6014. turns into this character followed by foo.");
  6015.   XSET (meta_prefix_char, Lisp_Int, 033);
  6016.  
  6017.   DEFVAR_LISP ("last-command", &last_command,
  6018.     "The last command executed.  Normally a symbol with a function definition,\n\
  6019. but can be whatever was found in the keymap, or whatever the variable\n\
  6020. `this-command' was set to by that command.");
  6021.   last_command = Qnil;
  6022.  
  6023.   DEFVAR_LISP ("this-command", &this_command,
  6024.     "The command now being executed.\n\
  6025. The command can set this variable; whatever is put here\n\
  6026. will be in `last-command' during the following command.");
  6027.   this_command = Qnil;
  6028.  
  6029.   DEFVAR_INT ("auto-save-interval", &auto_save_interval,
  6030.     "*Number of keyboard input characters between auto-saves.\n\
  6031. Zero means disable autosaving due to number of characters typed.");
  6032.   auto_save_interval = 300;
  6033.  
  6034.   DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
  6035.     "*Number of seconds idle time before auto-save.\n\
  6036. Zero or nil means disable auto-saving due to idleness.\n\
  6037. After auto-saving due to this many seconds of idle time,\n\
  6038. Emacs also does a garbage collection if that seems to be warranted.");
  6039.   XFASTINT (Vauto_save_timeout) = 30;
  6040.  
  6041.   DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
  6042.     "*Nonzero means echo unfinished commands after this many seconds of pause.");
  6043.   echo_keystrokes = 1;
  6044.  
  6045.   DEFVAR_INT ("polling-period", &polling_period,
  6046.     "*Interval between polling for input during Lisp execution.\n\
  6047. The reason for polling is to make C-g work to stop a running program.\n\
  6048. Polling is needed only when using X windows and SIGIO does not work.\n\
  6049. Polling is automatically disabled in all other cases.");
  6050.   polling_period = 2;
  6051.   
  6052.   DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
  6053.     "*Maximum time between mouse clicks to make a double-click.\n\
  6054. Measured in milliseconds.  nil means disable double-click recognition;\n\
  6055. t means double-clicks have no time limit and are detected\n\
  6056. by position only.");
  6057.   Vdouble_click_time = make_number (500);
  6058.  
  6059.   DEFVAR_INT ("num-input-keys", &num_input_keys,
  6060.     "*Number of complete keys read from the keyboard so far.");
  6061.   num_input_keys = 0;
  6062.  
  6063.   DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
  6064.     "*The frame in which the most recently read event occurred.\n\
  6065. If the last event came from a keyboard macro, this is set to `macro'.");
  6066.   Vlast_event_frame = Qnil;
  6067.  
  6068.   DEFVAR_LISP ("help-char", &Vhelp_char,
  6069.     "Character to recognize as meaning Help.\n\
  6070. When it is read, do `(eval help-form)', and display result if it's a string.\n\
  6071. If the value of `help-form' is nil, this char can be read normally.");
  6072.   XSET (Vhelp_char, Lisp_Int, Ctl ('H'));
  6073.  
  6074.   DEFVAR_LISP ("help-form", &Vhelp_form,
  6075.     "Form to execute when character `help-char' is read.\n\
  6076. If the form returns a string, that string is displayed.\n\
  6077. If `help-form' is nil, the help char is not recognized.");
  6078.   Vhelp_form = Qnil;
  6079.  
  6080.   DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
  6081.     "Command to run when `help-char' character follows a prefix key.\n\
  6082. This command is used only when there is no actual binding\n\
  6083. for that character after that prefix key.");
  6084.   Vprefix_help_command = Qnil;
  6085.  
  6086.   DEFVAR_LISP ("top-level", &Vtop_level,
  6087.     "Form to evaluate when Emacs starts up.\n\
  6088. Useful to set before you dump a modified Emacs.");
  6089.   Vtop_level = Qnil;
  6090.  
  6091.   DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
  6092.     "String used as translate table for keyboard input, or nil.\n\
  6093. Each character is looked up in this string and the contents used instead.\n\
  6094. If string is of length N, character codes N and up are untranslated.");
  6095.   Vkeyboard_translate_table = Qnil;
  6096.  
  6097.   DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
  6098.     "Keymap of key translations that can override keymaps.\n\
  6099. This keymap works like `function-key-map', but comes after that,\n\
  6100. and applies even for keys that have ordinary bindings.");
  6101.   Vkey_translation_map = Qnil;
  6102.  
  6103.   DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
  6104.     "Non-nil means to always spawn a subshell instead of suspending,\n\
  6105. even if the operating system has support for stopping a process.");
  6106.   cannot_suspend = 0;
  6107.  
  6108.   DEFVAR_BOOL ("menu-prompting", &menu_prompting,
  6109.     "Non-nil means prompt with menus when appropriate.\n\
  6110. This is done when reading from a keymap that has a prompt string,\n\
  6111. for elements that have prompt strings.\n\
  6112. The menu is displayed on the screen\n\
  6113. if X menus were enabled at configuration\n\
  6114. time and the previous event was a mouse click prefix key.\n\
  6115. Otherwise, menu prompting uses the echo area.");
  6116.   menu_prompting = 1;
  6117.  
  6118.   DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
  6119.     "Character to see next line of menu prompt.\n\
  6120. Type this character while in a menu prompt to rotate around the lines of it.");
  6121.   XSET (menu_prompt_more_char, Lisp_Int, ' ');
  6122.  
  6123.   DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
  6124.     "A mask of additional modifier keys to use with every keyboard character.\n\
  6125. Emacs applies the modifiers of the character stored here to each keyboard\n\
  6126. character it reads.  For example, after evaluating the expression\n\
  6127.     (setq extra-keyboard-modifiers ?\C-x)\n\
  6128. all input characters will have the control modifier applied to them.\n\
  6129. \n\
  6130. Note that the character ?\C-@, equivalent to the integer zero, does\n\
  6131. not count as a control character; rather, it counts as a character\n\
  6132. with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
  6133. cancels any modification.");
  6134.   extra_keyboard_modifiers = 0;
  6135.  
  6136.   DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
  6137.     "If an editing command sets this to t, deactivate the mark afterward.\n\
  6138. The command loop sets this to nil before each command,\n\
  6139. and tests the value when the command returns.\n\
  6140. Buffer modification stores t in this variable.");
  6141.   Vdeactivate_mark = Qnil;
  6142.  
  6143.   DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
  6144.     "Temporary storage of pre-command-hook or post-command-hook.");
  6145.   Vcommand_hook_internal = Qnil;
  6146.  
  6147.   DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
  6148.     "Normal hook run before each command is executed.\n\
  6149. While the hook is run, its value is temporarily set to nil\n\
  6150. to avoid an unbreakable infinite loop if a hook function gets an error.\n\
  6151. As a result, a hook function cannot straightforwardly alter the value of\n\
  6152. `pre-command-hook'.  See the Emacs Lisp manual for a way of\n\
  6153. implementing hook functions that alter the set of hook functions.");
  6154.   Vpre_command_hook = Qnil;
  6155.  
  6156.   DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
  6157.     "Normal hook run after each command is executed.\n\
  6158. While the hook is run, its value is temporarily set to nil\n\
  6159. to avoid an unbreakable infinite loop if a hook function gets an error.\n\
  6160. As a result, a hook function cannot straightforwardly alter the value of\n\
  6161. `post-command-hook'.  See the Emacs Lisp manual for a way of\n\
  6162. implementing hook functions that alter the set of hook functions.");
  6163.   Vpost_command_hook = Qnil;
  6164.  
  6165.   DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
  6166.     "t means menu bar, specified Lucid style, needs to be recomputed.");
  6167.   Vlucid_menu_bar_dirty_flag = Qnil;
  6168.  
  6169.   DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
  6170.     "List of menu bar items to move to the end of the menu bar.\n\
  6171. The elements of the list are event types that may have menu bar bindings.");
  6172.   Vmenu_bar_final_items = Qnil;
  6173.  
  6174.   DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
  6175.     "Keymap that overrides all other local keymaps.\n\
  6176. If this variable is non-nil, it is used as a keymap instead of the\n\
  6177. buffer's local map, and the minor mode keymaps and text property keymaps.");
  6178.   Voverriding_local_map = Qnil;
  6179.  
  6180.   DEFVAR_BOOL ("track-mouse", &do_mouse_tracking,
  6181.            "*Non-nil means generate motion events for mouse motion.");
  6182.  
  6183.   DEFVAR_LISP ("system-key-alist", &Vsystem_key_alist,
  6184.     "Alist of system-specific X windows key symbols.\n\
  6185. Each element should have the form (N . SYMBOL) where N is the\n\
  6186. numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
  6187. and SYMBOL is its name.");
  6188.   Vmenu_bar_final_items = Qnil;
  6189. }
  6190.  
  6191. keys_of_keyboard ()
  6192. {
  6193.   initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
  6194.   initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
  6195.   initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
  6196.   initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
  6197.   initial_define_key (meta_map, 'x', "execute-extended-command");
  6198. }
  6199.